HttpClient4.3.1

最近因业务需要,用HttpClient4.3.1版本进行业务开发,对HttpClient4.3.1研究了一下,但是网上对于这个版本的信息很少,缺少相关的文档资料,本文总结了HttpClient4.3.1相关用法。

1.创建HttpClient客户端

HttpClientBuilder clientbuilder = HttpClientBuilder.create();
CloseableHttpClient httpclient = clientbuilder.build();

2.发送Get请求

String url = "http://www.baidu.com";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
3.设置超时时间

RequestConfig requestconfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();
httpGet.setConfig(requestconfig);
4.获取响应信息实体、响应状态、响应信息

HttpEntity entity = httpResponse.getEntity();
StatusLine status = httpResponse.getStatusLine();
if(entity!=null){
	String data = EntityUtils.toString(entity);
}

简单的小例子:

	public static void main(String[] args) throws ClientProtocolException, IOException {
		
		//创建HttpClient客户端
		HttpClientBuilder clientbuilder = HttpClientBuilder.create();
		CloseableHttpClient httpclient = clientbuilder.build();
		
		String url = "http://www.baidu.com";
		HttpGet httpGet = new HttpGet(url);
		
		//设置超时时间
		RequestConfig requestconfig = RequestConfig.custom().setConnectTimeout(5000)
				.setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();
		httpGet.setConfig(requestconfig);
		
		try{
			//发送get请求
			HttpResponse httpResponse = httpclient.execute(httpGet);
			
			//获取响应消息实体
			HttpEntity entity = httpResponse.getEntity();
			
			//获取响应状态
			StatusLine status = httpResponse.getStatusLine();
			
			if(entity!=null){
				//获取响应信息
				String data = EntityUtils.toString(entity);
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
			httpclient.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}




HttpClient4.3.1 API 文档: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/overview-summary.html
HttpClient4.3.1相关jar包:http://pan.baidu.com/s/1qXN4Q6K


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值