天天看點

HttpClient使用詳解 一、簡介 二、特性 三、使用方法 四、執行個體



http協定的重要性相信不用我多說了,httpclient相比傳統jdk自帶的urlconnection,增加了易用性和靈活性(具體差別,日後我們再讨論),它不僅是用戶端發送http請求變得容易,而且也友善了開發人員測試接口(基于http協定的),即提高了開發的效率,也友善提高代碼的健壯性。是以熟練掌握httpclient是很重要的必修内容,掌握httpclient後,相信對于http協定的了解會更加深入。

httpclient是apache jakarta common下的子項目,用來提供高效的、最新的、功能豐富的支援http協定的用戶端程式設計工具包,并且它支援http協定最新的版本和建議。httpclient已經應用在很多的項目中,比如apache jakarta上很著名的另外兩個開源項目cactus和htmlunit都使用了httpclient。

1. 基于标準、純淨的java語言。實作了http1.0和http1.1

2. 以可擴充的面向對象的結構實作了http全部的方法(get, post, put, delete, head, options, and trace)。

3. 支援https協定。

4. 通過http代理建立透明的連接配接。

5. 利用connect方法通過http代理建立隧道的https連接配接。

6. basic, digest, ntlmv1, ntlmv2, ntlm2 session, snpnego/kerberos認證方案。

7. 插件式的自定義認證方案。

8. 便攜可靠的套接字工廠使它更容易的使用第三方解決方案。

9. 連接配接管理器支援多線程應用。支援設定最大連接配接數,同時支援設定每個主機的最大連接配接數,發現并關閉過期的連接配接。

10. 自動處理set-cookie中的cookie。

11. 插件式的自定義cookie政策。

12. request的輸出流可以避免流中内容直接緩沖到socket伺服器。

13. response的輸入流可以有效的從socket伺服器直接讀取相應内容。

14. 在http1.0和http1.1中利用keepalive保持持久連接配接。

15. 直接擷取伺服器發送的response code和 headers。

16. 設定連接配接逾時的能力。

17. 實驗性的支援http1.1 response caching。

18. 源代碼基于apache license 可免費擷取。

使用httpclient發送請求、接收響應很簡單,一般需要如下幾步即可。

1. 建立httpclient對象。

2. 建立請求方法的執行個體,并指定請求url。如果需要發送get請求,建立httpget對象;如果需要發送post請求,建立httppost對象。

3. 如果需要發送請求參數,可調用httpget、httppost共同的setparams(hetpparams params)方法來添加請求參數;對于httppost對象而言,也可調用setentity(httpentity entity)方法來設定請求參數。

4. 調用httpclient對象的execute(httpurirequest request)發送請求,該方法傳回一個httpresponse。

5. 調用httpresponse的getallheaders()、getheaders(string name)等方法可擷取伺服器的響應頭;調用httpresponse的getentity()方法可擷取httpentity對象,該對象包裝了伺服器的響應内容。程式可通過該對象擷取伺服器的響應内容。

6. 釋放連接配接。無論執行方法是否成功,都必須釋放連接配接

HttpClient使用詳解 一、簡介 二、特性 三、使用方法 四、執行個體

package com.test;  

import java.io.file;  

import java.io.fileinputstream;  

import java.io.ioexception;  

import java.io.unsupportedencodingexception;  

import java.security.keymanagementexception;  

import java.security.keystore;  

import java.security.keystoreexception;  

import java.security.nosuchalgorithmexception;  

import java.security.cert.certificateexception;  

import java.util.arraylist;  

import java.util.list;  

import javax.net.ssl.sslcontext;  

import org.apache.http.httpentity;  

import org.apache.http.namevaluepair;  

import org.apache.http.parseexception;  

import org.apache.http.client.clientprotocolexception;  

import org.apache.http.client.entity.urlencodedformentity;  

import org.apache.http.client.methods.closeablehttpresponse;  

import org.apache.http.client.methods.httpget;  

import org.apache.http.client.methods.httppost;  

import org.apache.http.conn.ssl.sslconnectionsocketfactory;  

import org.apache.http.conn.ssl.sslcontexts;  

import org.apache.http.conn.ssl.trustselfsignedstrategy;  

import org.apache.http.entity.contenttype;  

import org.apache.http.entity.mime.multipartentitybuilder;  

import org.apache.http.entity.mime.content.filebody;  

import org.apache.http.entity.mime.content.stringbody;  

import org.apache.http.impl.client.closeablehttpclient;  

import org.apache.http.impl.client.httpclients;  

import org.apache.http.message.basicnamevaluepair;  

import org.apache.http.util.entityutils;  

import org.junit.test;  

public class httpclienttest {  

    @test  

    public void junittest() {  

        get();  

    }  

    /** 

     * httpclient連接配接ssl 

     */  

    public void ssl() {  

        closeablehttpclient httpclient = null;  

        try {  

            keystore truststore = keystore.getinstance(keystore.getdefaulttype());  

            fileinputstream instream = new fileinputstream(new file("d:\\tomcat.keystore"));  

            try {  

                // 加載keystore d:\\tomcat.keystore    

                truststore.load(instream, "123456".tochararray());  

            } catch (certificateexception e) {  

                e.printstacktrace();  

            } finally {  

                try {  

                    instream.close();  

                } catch (exception ignore) {  

                }  

            }  

            // 相信自己的ca和所有自簽名的證書  

            sslcontext sslcontext = sslcontexts.custom().loadtrustmaterial(truststore, new trustselfsignedstrategy()).build();  

            // 隻允許使用tlsv1協定  

            sslconnectionsocketfactory sslsf = new sslconnectionsocketfactory(sslcontext, new string[] { "tlsv1" }, null,  

                    sslconnectionsocketfactory.browser_compatible_hostname_verifier);  

            httpclient = httpclients.custom().setsslsocketfactory(sslsf).build();  

            // 建立http請求(get方式)  

            httpget httpget = new httpget("https://localhost:8443/mydemo/ajax/serivcej.action");  

            system.out.println("executing request" + httpget.getrequestline());  

            closeablehttpresponse response = httpclient.execute(httpget);  

                httpentity entity = response.getentity();  

                system.out.println("----------------------------------------");  

                system.out.println(response.getstatusline());  

                if (entity != null) {  

                    system.out.println("response content length: " + entity.getcontentlength());  

                    system.out.println(entityutils.tostring(entity));  

                    entityutils.consume(entity);  

                response.close();  

        } catch (parseexception e) {  

            e.printstacktrace();  

        } catch (ioexception e) {  

        } catch (keymanagementexception e) {  

        } catch (nosuchalgorithmexception e) {  

        } catch (keystoreexception e) {  

        } finally {  

            if (httpclient != null) {  

                    httpclient.close();  

                } catch (ioexception e) {  

                    e.printstacktrace();  

        }  

     * post方式送出表單(模拟使用者登入請求) 

    public void postform() {  

        // 建立預設的httpclient執行個體.    

        closeablehttpclient httpclient = httpclients.createdefault();  

        // 建立httppost    

        httppost httppost = new httppost("http://localhost:8080/mydemo/ajax/serivcej.action");  

        // 建立參數隊列    

        list<namevaluepair> formparams = new arraylist<namevaluepair>();  

        formparams.add(new basicnamevaluepair("username", "admin"));  

        formparams.add(new basicnamevaluepair("password", "123456"));  

        urlencodedformentity uefentity;  

            uefentity = new urlencodedformentity(formparams, "utf-8");  

            httppost.setentity(uefentity);  

            system.out.println("executing request " + httppost.geturi());  

            closeablehttpresponse response = httpclient.execute(httppost);  

                    system.out.println("--------------------------------------");  

                    system.out.println("response content: " + entityutils.tostring(entity, "utf-8"));  

        } catch (clientprotocolexception e) {  

        } catch (unsupportedencodingexception e1) {  

            e1.printstacktrace();  

            // 關閉連接配接,釋放資源    

                httpclient.close();  

            } catch (ioexception e) {  

     * 發送 post請求通路本地應用并根據傳遞參數不同傳回不同結果 

    public void post() {  

        formparams.add(new basicnamevaluepair("type", "house"));  

     * 發送 get請求 

    public void get() {  

            // 建立httpget.    

            httpget httpget = new httpget("http://www.baidu.com/");  

            system.out.println("executing request " + httpget.geturi());  

            // 執行get請求.    

                // 擷取響應實體    

                system.out.println("--------------------------------------");  

                // 列印響應狀态    

                    // 列印響應内容長度    

                    // 列印響應内容    

                    system.out.println("response content: " + entityutils.tostring(entity));  

                system.out.println("------------------------------------");  

     * 上傳檔案 

    public void upload() {  

            httppost httppost = new httppost("http://localhost:8080/mydemo/ajax/serivcefile.action");  

            filebody bin = new filebody(new file("f:\\image\\sendpix0.jpg"));  

            stringbody comment = new stringbody("a binary file of some kind", contenttype.text_plain);  

            httpentity reqentity = multipartentitybuilder.create().addpart("bin", bin).addpart("comment", comment).build();  

            httppost.setentity(reqentity);  

            system.out.println("executing request " + httppost.getrequestline());  

                httpentity resentity = response.getentity();  

                if (resentity != null) {  

                    system.out.println("response content length: " + resentity.getcontentlength());  

                entityutils.consume(resentity);  

}</namevaluepair></namevaluepair></namevaluepair></namevaluepair>  

本執行個體是采用httpclient4.3最新版本。該版本與之前的代碼寫法風格相差較大,大家多留意下。