JAVA: httpclient 详解——第七章;


httpclient 详解——第一章;

httpclient 详解——第二章;

httpclient 详解——第三章;

httpclient 详解——第四章; 

httpclient 详解——第五章;

httpclient 详解——第六章;

httpclient 详解——第七章;




相对于httpurlconnection ,httpclient更加丰富,也更加强大,其中apache有两个项目都是httpclient,一个是commonts包下的,这个是通用的,更专业的是org.apache.http.包下的,所以我一般用后者;


httpclient可以处理长连接,保存会话,重连接,以及请求过滤器,连接重用等等...


下面是测试代码(全部总结来自官方文档,以及翻译)


须要下载核心包:httpclient-4.3.4.jar ,也可在官网下载:http://hc.apache.org/downloads.cgi



/**
	 * 
	 */
	private static void test26() throws ClientProtocolException, IOException{
//		CloseableHttpClient httpclient = HttpClients.createDefault();  
//		HttpClientContext context = HttpClientContext.create();  
//		HttpGet httpget = new HttpGet("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
//		CloseableHttpResponse response = httpclient.execute(httpget, context);  
//		try {  
//		    Principal principal = context.getUserToken(Principal.class);  
//		    System.out.println(principal);  
//		} finally {  
//		    response.close();  
//		}
		
		
		
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		HttpClientContext context1 = HttpClientContext.create();  
		HttpGet httpget1 = new HttpGet("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
		CloseableHttpResponse response1 = httpclient.execute(httpget1, context1);  
		try {  
		    HttpEntity entity1 = response1.getEntity();  
		} finally {
		    response1.close();  
		}  
		Principal principal = context1.getUserToken(Principal.class);  
		
		HttpClientContext context2 = HttpClientContext.create();  
		context2.setUserToken(principal);  
		HttpGet httpget2 = new HttpGet("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
		CloseableHttpResponse response2 = httpclient.execute(httpget2, context2);  
		try {  
		    HttpEntity entity2 = response2.getEntity();  
		} finally {  
		    response2.close();  
		}
		
		
	}
	
	


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值