码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA: httpclient 详解——第七章;

时间:2014-08-22 16:22:39      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:java   httpclient   http协议   


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();  
		}
		
		
	}
	
	


JAVA: httpclient 详解——第七章;

标签:java   httpclient   http协议   

原文地址:http://blog.csdn.net/lyc66666666666/article/details/38757563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!