`
seattle2881
  • 浏览: 32738 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

HttpClient4入门应用之二----Post方法访问Action带传参

阅读更多

Struts2的Action配置代码

 

 

<package name="Ajax" extends="json-default" namespace="/Ajax">
    <action name="serivceJ" class="com.wanghe.test.TestAction" method="serivceJ" >
   		<result  type="json"></result>
	</action>
</package>

 

Action代码

 

 

public void serivceJ() {
        try {
    HttpServletResponse response = ServletActionContext.getResponse(); 
HttpServletRequest request = ServletActionContext.getRequest();
 System.out.println("request...serivceJ");
	    response.setCharacterEncoding("UTF-8");
	        String type = request.getParameter("type");
	        String c = "none";
	        if(type.equalsIgnoreCase("girl")){
	        	c = "女孩你好!";
	        }else if(type.equalsIgnoreCase("boy")){
	        	c = "男孩你好!";
	        }
			response.getWriter().write(c);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

HttpClient测试代码

 

//创建默认的httpClient实例.
HttpClient httpclient = new DefaultHttpClient();
//创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
//创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("type", "girl"));
UrlEncodedFormEntity uefEntity;
try {
	uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
	httppost.setEntity(uefEntity);
	System.out.println("executing request " + httppost.getURI());
	HttpResponse response;
	response = httpclient.execute(httppost);
	HttpEntity entity = response.getEntity();
	if (entity != null) {		System.out.println("--------------------------------------");
	System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8"));
System.out.println("--------------------------------------");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		}catch(UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
             //关闭连接,释放资源
			httpclient.getConnectionManager().shutdown();
		}

 

输出:

 

executing request http://localhost:8080/myDemo/Ajax/serivceJ.action
--------------------------------------
Response content: 女孩你好!
--------------------------------------
分享到:
评论

相关推荐

    httpclient-4.5.13-API文档-中文版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpclient-4.5.6-API文档-中文版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.13-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpclient 4.X版本下载有的带源码包

    2013-11-21 15:31 292,890 httpclient-4.0.2.jar 2017-12-20 12:08 351,132 httpclient-4.1.1.jar 2012-08-03 01:45 451,595 httpclient-4.1.2-sources.jar 2012-08-03 01:44 352,254 httpclient-4.1.2.jar 2012-08-...

    httpclient-4.5.12-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...

    httpclient-4.5.6-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    commons-httpclient3.1.jar,commons-codec1.3.jar,commons-logging1.1.1.jar

    标题中的"commons-httpclient3.1.jar,commons-codec1.3.jar,commons-logging1.1.1.jar"指的是三个关键的Java库文件,它们是Apache HttpClient项目的一部分,用于在Java应用程序中实现HTTP通信。这些JAR(Java ...

    c#实现HttpClient拼接multipart/form-data形式参数post提交数据

    使用c#实现的HttpClient拼接multipart/form-data形式参数post提交数据,包含图片内容,有需要的可以下载,希望能帮到有需要的人,

    httpclient-4.5.5-API文档-中文版.zip

    赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...

    httpclient-4.5.13.jar中文-英文对照文档.zip

    ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件...

    httpclient-4.5.2-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...

    httpclient-4.4.1-API文档-中文版.zip

    赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.10-API文档-中文版.zip

    赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...

    commons-httpclient-3.0.jar JAVA中使用HttpClient可以用到

    《JAVA中使用HttpClient:commons-httpclient-3.0.jar详解》 在JAVA开发中,进行HTTP请求时,Apache的HttpClient库是一个不可或缺的工具。本文将深入解析`commons-httpclient-3.0.jar`,它是HttpClient的一个重要...

    httpclient-4.2.5-API文档-中文版.zip

    赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...

    wechatpay-apache-httpclient-0.2.1.jar

    wechatpay-apache-httpclient-0.2.1.jar

    httpclient-4.4-API文档-中文版.zip

    赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...

    httpclient-4.5.2-API文档-中文版.zip

    赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)版.zip ...

    httpclient-4.5.3-API文档-中文版.zip

    赠送jar包:httpclient-4.5.3.jar 赠送原API文档:httpclient-4.5.3-javadoc.jar 赠送源代码:httpclient-4.5.3-sources.jar 包含翻译后的API文档:httpclient-4.5.3-javadoc-API文档-中文(简体)版.zip 对应Maven...

    httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载

    例如,当需要发送包含文件或其他二进制数据的POST请求时,这个库就显得尤为重要。它提供了类和方法来创建和解析复杂的Multipart MIME消息,这是上传文件或发送包含多个部分的HTTP请求所必需的。 这些jar包通常被...

Global site tag (gtag.js) - Google Analytics