JAVA: httpclient 具體解釋——第五章;



httpclient 具體解釋——第一章;
java

httpclient 具體解釋——第二章;web

httpclient 具體解釋——第三章;apache

httpclient 具體解釋——第四章; api

httpclient 具體解釋——第五章;緩存

httpclient 具體解釋——第六章;安全

httpclient 具體解釋——第七章;



相對於httpurlconnection ,httpclient更加豐富,也更增強大,當中apache有兩個項目都是httpclient,一個是commonts包下的,這個是通用的,更專業的是org.apache.http.包下的,因此我通常用後者;socket


httpclient可以處理長鏈接,保存會話,重鏈接,以及請求過濾器,鏈接重用等等...post


如下是測試代碼(全部總結來自官方文檔,以及翻譯)
ui


須要下載核心包:httpclient-4.3.4.jar ,也可在官網下載:http://hc.apache.org/downloads.cgiurl






//--------------------------  高速API ---------------------------------
	
	
	/**
	 * 高速api僅僅提供最主要的功能,僅僅用於不須要靈活擴展的場景
	 */
	private static void test22() throws ClientProtocolException, IOException{
		
		String result = Request.Get("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo")
				.connectTimeout(1000)//設置server請求超時
				.socketTimeout(1000)//設置server對應超時
				.execute()
				.returnContent()
				.asString();
		
		String result2 = Request.Post("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo")
				.useExpectContinue()
				.version(HttpVersion.HTTP_1_1)
				.bodyString("參數", ContentType.DEFAULT_TEXT)
				.execute()
				.returnContent()
				.asString();
		
		//提交HTML表單 ,並保存返回結果
		Request.Post("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo")  
		        .addHeader("X-Custom-header", "stuff") // 表單頭
		        .viaProxy(new HttpHost("myproxy", 8080))  // 設置代理
		        .bodyForm(Form.form()   //表單
					          .add("mobileCode", "12345")
					          .add("userID", "123456")
					          .build())
		        .execute()    
		        .saveContent(new File("result.txt")); 
		
		System.out.println(result);
		System.out.println(result2);

	}
	
	
	/**
	 * 利用Executor 高速開發;
	 * 假設須要在指定的安全上下文中運行某些請求,咱們也可以直接使用Exector,
	 * 這時候用戶的認證信息就會被緩存起來,以便興許的請求使用。
	 */
	private static void test23() throws ClientProtocolException, IOException{
		
		
	    Executor executor = Executor.newInstance()  
	            .auth(new HttpHost("somehost",8080), "username", "password")//加入認證
	            .authPreemptive(new HttpHost("somehost", 8080));  //使用搶先認證
	    
	    executor.execute(Request.Get("http://somehost/"))//運行get請求
	            .returnContent().asString();
	    
	    executor.execute(Request.Post("http://somehost/") //運行post請求 
	            .useExpectContinue()
	            .bodyString("Important stuff", ContentType.DEFAULT_TEXT))
	            .returnContent().asString();
	    
	}
	
	
	
	/**
	 * 
	 * 高速響應處理
	 * 
	 * 利用request高速發送get請求,並用ResponseHandler 回調返回結果;
	 */
	private static void test24() throws ClientProtocolException, IOException {

		Object result = Request .Get("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo")
				.execute().handleResponse(new ResponseHandler<Object>() {
					
					public Object handleResponse(final HttpResponse response) throws IOException {

						StatusLine statusLine = response.getStatusLine();
						
						if(statusLine.getStatusCode()==200){
							
							HttpEntity entity = response.getEntity();
							if (entity != null) {
								String str = EntityUtils.toString(entity);
								return str;
							}
						}
						
						return null;
					}
					
				});
		
		
		if (result != null) {
			System.out.println(">>>>>>"+result);
		}

	}
	
	
相關文章
相關標籤/搜索