在web开发中,我们经常需要模拟post及get请求,现在网上比较多的是使用httpclient3.x,然而httpclient4.x已经发布好几年了,而且4.x之后改名为HttpComponents,显然是今后的趋势.
Apache HttpComponents4.x中的HttpClient是一个很好的工具,它符合HTTP1.1规范,是基于HttpCore类包的实现。但是HttpComponents4.x较之前httpclient3.x的API变化比较大,已经分为HttpClient,HttpCore,HttpAsyncClient等多个组件,在模拟post及get请求时的编码也出现了较大的变化.
下面是httpclient4.3.4模拟get请求的例程
public void requestGet(String urlWithParams) throws Exception {
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//HttpGet httpget = new HttpGet("http://www.baidu.com/");
HttpGet httpget = new HttpGet(urlWithParams);
//配置请求的超时设置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(50)
.setConnectTimeout(50)
.setSocketTimeout(50).build();
httpget.setConfig(requestConfig);
CloseableHttpResponse response = httpclient.execute(httpget);
System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils.toString(entity);//, "utf-8");
System.out.println(jsonStr);
httpget.releaseConnection();
}
httpclient4.3.4模拟post请求的例程
public void requestPost(String url,List<NameValuePair> params) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpclient.execute(httppost);
System.out.println(response.toString());
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils.toString(entity, "utf-8");
System.out.println(jsonStr);
httppost.releaseConnection();
}
运行post方法时,可以
public static void main(String[] args){
try {
String loginUrl = "http://localhost:8080/yours";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "zhang"));
params.add(new BasicNameValuePair("passwd", "123"));
requestPost(loginUrl,params);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
最后给出httpcomponents官网的例程
http://hc.apache.org/httpcomponents-core-4.3.x/examples.html
分享到:
相关推荐
httpclient4.3.x和其依赖jar包。
NULL 博文链接:https://chun521521.iteye.com/blog/2422270
帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x
httpclient-4.3.jar apache http 欢迎下载
Apache Httpclient4.3.x 系列jar套件
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-...
httpmime-4.3.jar
httpclient4.3的二次封装,解决了代理问题、多线程问题、SSL问题、通用的get、post请求
HttpClient 4.3教程.rar
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
这个压缩包包含了HttpClient4.3中文版pdf和英文版pdf 。word文档里也有几个学习的网址。如果用HttpClient4.5的话上面的知识基本(95%)能用的上。省去了到处找零碎资料的时间。
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
httpclient-android-4.3.5.1.jar,用于解决Android使用高版本httpclient出错的问题,下载完直接放到libs文件夹下,然后右键-build path-add to build path,然后在buildpath configuration 里面勾上,就搞定了
httpclient4.3开发的工具类,httpclientUtils 因为项目需要,自己就根据常用的写了一个。
httpclient4.3所需jar包
httpClient4.3官方包,官方源文件,可以直接使用
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
提供HttpClient4.3所需要的jar
httpclient 4.x 与httpclient3.x已经有很大的区别 该资源简单可用 需要下载最新的httpclient4.1.1包及相关的资源包即可使用
httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4