<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】 ? ? ?搜了一下網絡上別人封裝的HttpClient,大部分特別簡單,有一些看起來比較高級,但是用起來都不怎么好用。調用關系不清楚,結構有點混亂。所以也就萌生了自己封裝HttpClient工具類的想法。要做就做最好的,本工具類支持插件式配置Header、插件式配置httpclient對象,這樣就可以方便地自定義header信息、配置ssl、配置proxy等。 ? ? ?是不是覺得說的有點懸乎了,那就先看看調用吧: ~~~ public static void testSimple() throws HttpProcessException{ String url = "http://www.oschina.net"; //簡單調用 String resp = HttpClientUtil.send(url); System.out.println("請求結果內容長度:"+ resp.length()); } public static void testOne() throws HttpProcessException{ String url = "https://sso.tgb.com:8443/cas/login"; //自定義HttpClient,設置超時、代理、ssl //HttpClient client= HCB.custom().timeout(10000).proxy("127.0.0.1", 8087).ssl().build();//采用默認方式(繞過證書驗證) HttpClient client= HCB.custom().timeout(10000).ssl("D:\\keys\\wsriakey","tomcat").build(); //設置header信息 Header[] headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build(); //執行請求 String resp=HttpClientUtil.send(client, url, headers); System.out.println("請求結果如下:"); System.out.println(resp); } ~~~ ? ? ?輕松配置了代理、自定義證書的ssl、以及各種header頭信息,是不是覺得還湊合呢,那就繼續看吧。 ? ? ?寫這個工具類時,抽象了一下所有的demo,最后封裝了一個最基本的方法(拆分成了2個方法了),其所有參數列表有:HttpClient對象、url(必須有)、請求方式、請求參數parasMap、header數組、編碼格式encoding。 ? ? ?由于封裝的是工具類,所以最好是無狀態的,可以支持多線程的方式調用的,所以方法都是static類型的。這也是為什么要把HttpClient對象也是作為了一個參數傳入而非成員變量了,而且這樣也為擴展HttpClient的配置提供了便利。 ? ? ?因為HTTP1.1規范中定義了6種HTTP方法:GET, HEAD, POST, PUT, DELETE, TRACE 和 OPTIONS,其實還有一個PATCH,這幾個方法在HttpClient中都有一個對應的類:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace、HttpOptions以及HttpPatch。所有的這些類均繼承了HttpRequestBase超類,故可以作為參數使用(用枚舉類作為參數,用另一個方法來創建具體的請求方法對象)。 ? ? ?Header頭信息也是作為一個重要的參數,在請求特定網站的時候需要設置不同的Header,而header又是比較繁雜的,所以這里也是作為了一個參數傳入的,也是方便擴展。 ? ? ?使用map來作為post方式傳入參數是習慣使然,不做過多的解釋。 ? ? ?編碼這個參數主要是為了為待提交的數據和反饋結果進行轉碼處理。 ? ? ?簡單說一下流程: 1. 創建請求對象request; 2. 為request設置header信息; 3. 判斷當前請求對象是否是HttpEntityEnclosingRequestBase的子類,如果是,則支持setEntity方法,來設置參數。 4. 執行請求,并拿到結果(同步阻塞); 5. 獲取并解碼請求結果實體; 6. 關閉鏈接 ? ? ?就是這么簡單,具體來看看代碼吧: ~~~ /** * 請求資源或服務,自定義client對象,傳入請求參數,設置內容類型,并指定參數和返回數據的編碼 * * @param client client對象 * @param url 資源地址 * @param httpMethod 請求方法 * @param parasMap 請求參數 * @param headers 請求頭信息 * @param encoding 編碼 * @return 返回處理結果 * @throws HttpProcessException */ public static String send(HttpClient client, String url, HttpMethods httpMethod, Map<String,String>parasMap,? Header[] headers, String encoding) throws HttpProcessException { String body = ""; try { //創建請求對象 HttpRequestBase request = getRequest(url, httpMethod); //設置header信息 request.setHeaders(headers); //判斷是否支持設置entity(僅HttpPost、HttpPut、HttpPatch支持) if(HttpEntityEnclosingRequestBase.class.isAssignableFrom(request.getClass())){ List<NameValuePair> nvps = new ArrayList<NameValuePair>(); //檢測url中是否存在參數 url = Utils.checkHasParas(url, nvps); //裝填參數 Utils.map2List(nvps, parasMap); //設置參數到請求對象中 ((HttpEntityEnclosingRequestBase)request).setEntity(new UrlEncodedFormEntity(nvps, encoding)); logger.debug("請求地址:"+url); if(nvps.size()>0){ logger.debug("請求參數:"+nvps.toString()); } }else{ int idx = url.indexOf("?"); logger.debug("請求地址:"+url.substring(0, (idx>0 ? idx-1:url.length()-1))); if(idx>0){ logger.debug("請求參數:"+url.substring(idx+1)); } } //調用發送請求 body = execute(client, request, url, encoding); } catch (UnsupportedEncodingException e) { throw new HttpProcessException(e); } return body; } /** * 請求資源或服務 * * @param client client對象 * @param request 請求對象 * @param url 資源地址 * @param parasMap 請求參數 * @param encoding 編碼 * @return 返回處理結果 * @throws HttpProcessException */ private static String execute(HttpClient client, HttpRequestBase request,String url, String encoding) throws HttpProcessException { String body = ""; HttpResponse response =null; try { //執行請求操作,并拿到結果(同步阻塞) response = client.execute(request); //獲取結果實體 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定編碼轉換結果實體為String類型 body = EntityUtils.toString(entity, encoding); logger.debug(body); } EntityUtils.consume(entity); } catch (ParseException | IOException e) { throw new HttpProcessException(e); } finally { close(response); } return body; } ~~~ ? ? ?第一個方法中,我們看到有HttpMethods類型的參數,在創建request對象時,用到了它。它是什么呢?其實只是一個枚舉類: ~~~ /** * 枚舉HttpMethods方法 * * @author arron * @date 2015年11月17日 下午4:45:59 * @version 1.0 */ public enum HttpMethods{ /** * 求獲取Request-URI所標識的資源 */ GET(0, "GET"), /** * 向指定資源提交數據進行處理請求(例如提交表單或者上傳文件)。數據被包含在請求體中。 * POST請求可能會導致新的資源的建立和/或已有資源的修改 */ POST(1, "POST"), /** * 向服務器索要與GET請求相一致的響應,只不過響應體將不會被返回。 * 這一方法可以在不必傳輸整個響應內容的情況下,就可以獲取包含在響應消息頭中的元信息 * 只獲取響應信息報頭 */ HEAD(2, "HEAD"), /** * 向指定資源位置上傳其最新內容(全部更新,操作冪等) */ PUT (3, "PUT"), /** * 請求服務器刪除Request-URI所標識的資源 */ DELETE (4, "DELETE"), /** * 請求服務器回送收到的請求信息,主要用于測試或診斷 */ TRACE(5, "TRACE"), /** * 向指定資源位置上傳其最新內容(部分更新,非冪等) */ PATCH (6, "PATCH"), /** * 返回服務器針對特定資源所支持的HTTP請求方法。 * 也可以利用向Web服務器發送'*'的請求來測試服務器的功能性 */ OPTIONS (7, "OPTIONS"), // /** // * HTTP/1.1協議中預留給能夠將連接改為管道方式的代理服務器 // */ // CONNECT(99, "CONNECT"), ; private int code; private String name; private HttpMethods(int code, String name){ this.code = code; this.name = name; } public String getName() { return name; } public int getCode() { return code; } } ~~~ ? ? ?通過getRequest方法,來實例化對應方法的請求對象。 ~~~ /** * 根據請求方法名,獲取request對象 * * @param url 資源地址 * @param method 請求方式 * @return */ private static HttpRequestBase getRequest(String url, HttpMethods method) { HttpRequestBase request = null; switch (method.getCode()) { case 0:// HttpGet request = new HttpGet(url); break; case 1:// HttpPost request = new HttpPost(url); break; case 2:// HttpHead request = new HttpHead(url); break; case 3:// HttpPut request = new HttpPut(url); break; case 4:// HttpDelete request = new HttpDelete(url); break; case 5:// HttpTrace request = new HttpTrace(url); break; case 6:// HttpPatch request = new HttpPatch(url); break; case 7:// HttpOptions request = new HttpOptions(url); break; default: request = new HttpPost(url); break; } return request; } ~~~ ? ? ?當然最后的關閉鏈接也是一個小方法: ~~~ /** * 嘗試關閉response * * @param resp HttpResponse對象 */ private static void close(HttpResponse resp) { try { if(resp == null) return; //如果CloseableHttpResponse 是resp的父類,則支持關閉 if(CloseableHttpResponse.class.isAssignableFrom(resp.getClass())){ ((CloseableHttpResponse)resp).close(); } } catch (IOException e) { logger.error(e); } } ~~~ ? ? ?當然各種參數的組合方法也簡單提供一下(為了節約空間,已去掉注釋): ~~~ public static String send(String url) throws HttpProcessException { return send(url, Charset.defaultCharset().name()); } public static String send(String url, String encoding) throws HttpProcessException { return send(url, new Header[]{},encoding); } public static String send(String url, Header[] headers) throws HttpProcessException { return send(url, headers, Charset.defaultCharset().name()); } public static String send(String url, Header[] headers, String encoding) throws HttpProcessException { return send(url, new HashMap<String,String>(), headers, encoding); } public static String send(String url, Map<String,String>parasMap) throws HttpProcessException { return send(url, parasMap, Charset.defaultCharset().name()); } public static String send(String url, Map<String,String>parasMap, String encoding) throws HttpProcessException { return send(url, parasMap, new Header[]{}, encoding); } public static String send(String url, Map<String,String>parasMap, Header[] headers) throws HttpProcessException { return send(url, parasMap, headers, Charset.defaultCharset().name()); } public static String send(String url, Map<String,String>parasMap, Header[] headers, String encoding) throws HttpProcessException { return send(url, HttpMethods.POST, parasMap, headers, encoding); } public static String send(String url, HttpMethods httpMethod) throws HttpProcessException { return send(url, httpMethod, Charset.defaultCharset().name()); } public static String send(String url, HttpMethods httpMethod, String encoding) throws HttpProcessException { return send(url, httpMethod, new Header[]{},encoding); } public static String send(String url, HttpMethods httpMethod, Header[] headers) throws HttpProcessException { return send(url, httpMethod, headers, Charset.defaultCharset().name()); } public static String send(String url, HttpMethods httpMethod, Header[] headers, String encoding) throws HttpProcessException { return send(url, httpMethod, new HashMap<String, String>(), headers, encoding); } public static String send(String url, HttpMethods httpMethod, Map<String,String>parasMap) throws HttpProcessException { return send(url, httpMethod, parasMap, Charset.defaultCharset().name()); } public static String send(String url, HttpMethods httpMethod, Map<String,String>parasMap, String encoding) throws HttpProcessException { return send(url, httpMethod, parasMap, new Header[]{}, encoding); } public static String send(String url, HttpMethods httpMethod, Map<String,String>parasMap, Header[] headers) throws HttpProcessException { return send(url, httpMethod, parasMap, headers, Charset.defaultCharset().name()); } public static String send(String url, HttpMethods httpMethod, Map<String,String>parasMap, Header[] headers, String encoding) throws HttpProcessException { return send(create(url), url, httpMethod, parasMap, headers, encoding); } public static String send(HttpClient client, String url) throws HttpProcessException { return send(client, url, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, String encoding) throws HttpProcessException { return send(client, url, new Header[]{}, encoding); } public static String send(HttpClient client, String url, Header[] headers) throws HttpProcessException { return send(client, url, headers, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, Header[] headers, String encoding) throws HttpProcessException { return send(client, url, new HashMap<String, String>(), headers, encoding); } public static String send(HttpClient client, String url, Map<String,String>parasMap) throws HttpProcessException { return send(client, url, parasMap, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, Map<String,String>parasMap, String encoding) throws HttpProcessException { return send(client, url, parasMap, new Header[]{}, encoding); } public static String send(HttpClient client, String url, Map<String,String>parasMap, Header[] headers) throws HttpProcessException { return send(client, url, parasMap, headers, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.POST, parasMap, headers, encoding); } public static String send(HttpClient client, String url, HttpMethods httpMethod) throws HttpProcessException { return send(client, url, httpMethod, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, HttpMethods httpMethod, String encoding) throws HttpProcessException { return send(client, url, httpMethod, new Header[]{}, encoding); } public static String send(HttpClient client, String url, HttpMethods httpMethod, Header[] headers) throws HttpProcessException { return send(client, url, httpMethod, headers, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, HttpMethods httpMethod, Header[] headers, String encoding) throws HttpProcessException { return send(client, url, httpMethod, new HashMap<String, String>(), headers, encoding); } public static String send(HttpClient client, String url, HttpMethods httpMethod, Map<String,String>parasMap) throws HttpProcessException { return send(client, url, httpMethod, parasMap, Charset.defaultCharset().name()); } public static String send(HttpClient client, String url, HttpMethods httpMethod, Map<String,String>parasMap, String encoding) throws HttpProcessException { return send(client, url, httpMethod, parasMap, new Header[]{}, encoding); } public static String send(HttpClient client, String url, HttpMethods httpMethod, Map<String,String>parasMap, Header[] headers) throws HttpProcessException { return send(client, url, httpMethod, parasMap, headers, Charset.defaultCharset().name()); } ~~~ ? ? ?可以看到上面這一堆方法,其實主要分成2類,一類是傳入client對象的,一組是沒有傳入的。也就是說該工具類提供了一種默認的client對象。這個將會在下一篇文章會有補充。 ? ? ?當然,為了方便操作,還是提供了get、post、put、patch、delete、head、options、trace等方法,由于推薦使用send方法,所以這幾個方法只是做了一個簡單的調用: ~~~ public static String get(String url, Header[] headers,String encoding) throws HttpProcessException { return get(create(url), url, headers, encoding); } public static String get(HttpClient client, String url, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.GET, headers, encoding); } public static String post(String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return post(create(url), url, parasMap, headers, encoding); } public static String post(HttpClient client, String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.POST, parasMap, headers, encoding); } public static String put(String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return put(create(url), url, parasMap, headers, encoding); } public static String put(HttpClient client, String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.PUT, parasMap, headers, encoding); } public static String delete(String url, Header[] headers,String encoding) throws HttpProcessException { return delete(create(url), url, headers, encoding); } public static String delete(HttpClient client, String url, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.DELETE, headers, encoding); } public static String patch(String url, Map<String,String>parasMap,Header[] headers,String encoding) throws HttpProcessException { return patch(create(url), url, parasMap, headers, encoding); } public static String patch(HttpClient client, String url, Map<String,String>parasMap, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.PATCH, parasMap, headers, encoding); } public static String head(String url, Header[] headers,String encoding) throws HttpProcessException { return head(create(url), url, headers, encoding); } public static String head(HttpClient client, String url, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.HEAD, headers, encoding); } public static String options(String url, Header[] headers,String encoding) throws HttpProcessException { return options(create(url), url, headers, encoding); } public static String options(HttpClient client, String url, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.OPTIONS, headers, encoding); } public static String trace(String url, Header[] headers,String encoding) throws HttpProcessException { return trace(create(url), url, headers, encoding); } public static String trace(HttpClient client, String url, Header[] headers,String encoding) throws HttpProcessException { return send(client, url, HttpMethods.TRACE, headers, encoding); } ~~~ ? ? ?差點忘記了,最后還有一個簡單的通用工具類 ~~~ /** * * @author arron * @date 2015年11月10日 下午12:49:26 * @version 1.0 */ public class Utils { /** * 檢測url是否含有參數,如果有,則把參數加到參數列表中 * * @param url 資源地址 * @param nvps 參數列表 * @return 返回去掉參數的url */ public static String checkHasParas(String url, List<NameValuePair> nvps) { // 檢測url中是否存在參數 if (url.contains("?") && url.indexOf("?") < url.indexOf("=")) { Map<String, String> map = buildParas(url.substring(url .indexOf("?") + 1)); map2List(nvps, map); url = url.substring(0, url.indexOf("?")); } return url; } /** * 參數轉換,將map中的參數,轉到參數列表中 * * @param nvps 參數列表 * @param map 參數列表(map) */ public static void map2List(List<NameValuePair> nvps, Map<String, String> map) { if(map==null) return; // 拼接參數 for (Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry .getValue())); } } /** * 生成參數 * 參數格式“k1=v1&k2=v2” * * @param paras 參數列表 * @return 返回參數列表(map) */ public static Map<String,String> buildParas(String paras){ String[] p = paras.split("&"); String[][] ps = new String[p.length][2]; int pos = 0; for (int i = 0; i < p.length; i++) { pos = p[i].indexOf("="); ps[i][0]=p[i].substring(0,pos); ps[i][1]=p[i].substring(pos+1); pos = 0; } return buildParas(ps); } /** * 生成參數 * 參數類型:{{"k1","v1"},{"k2","v2"}} * * @param paras 參數列表 * @return 返回參數列表(map) */ public static Map<String,String> buildParas(String[][] paras){ // 創建參數隊列 Map<String,String> map = new HashMap<String, String>(); for (String[] para: paras) { map.put(para[0], para[1]); } return map; } } ~~~ ? ? ?簡單的封裝就是這樣了。 ? ? ?由于HttpClient和Header都作為參數傳入,所以也可以進行擴展,比如代理、ssl等都是對HttpClient進行配置的,下面的文章就分別分享一下如何插件式配置HttpClient以及Header。敬請期待。 ? ? ?代碼已上傳至:[https://github.com/Arronlong/httpclientUtil](https://github.com/Arronlong/httpclientUtil)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看