<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】 ? ? ?前面的文章介紹了一些HttpClient的簡單示例,本文繼續采用示例的方式來演示HttpClient的功能。 ? ? ?在項目中我們可能會遇到這樣的情況:為了保證系統的安全性,只允許使用特定的代理才可以訪問,而與這些系統使用HttpClient進行交互時,只能為其配置代理。 ? ? ?這里我們使用goagent代理訪問臉書來模擬這種情況。facebook由于某些原因被封,只能通過代理來訪問,所以正好也符合我們現在的演示需求。現在在瀏覽器上訪問是可以訪問的: ![](https://box.kancloud.cn/2016-02-18_56c53cb7c41ee.jpg) ? ? ?可以看到facebook采用的也是https的方式,而且該網站的證書不客戶端被信任。所以我們要采用“繞過證書驗證”的方式使用Https。那如何設置代理呢,官網有[相關的示例](http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java)。我采用的跟官網提供的稍微不一樣,具體代碼如下: ~~~ /** * 設置代理 * @param builder * @param hostOrIP * @param port */ public static HttpClientBuilder proxy(String hostOrIP, int port){ // 依次是代理地址,代理端口號,協議類型 HttpHost proxy = new HttpHost(hostOrIP, port, "http"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); return HttpClients.custom().setRoutePlanner(routePlanner); } ~~~ ? ? ?返回值是HttpClientBuilder,這個類是用來生成HttpClient對象的,同時可以設置各種參數,這里提供返回值是為了配置代理后,繼續配置ssl。打開goagent,看看代理ip的設定如圖: ![](https://box.kancloud.cn/2016-02-18_56c53cb7e21ef.jpg) ? ? ?現在修改send方法: ~~~ /** * 模擬請求 * * @param url 資源地址 * @param map 參數列表 * @param encoding 編碼 * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws IOException * @throws ClientProtocolException */ public static String send(String url, Map<String,String> map,String encoding) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException { String body = ""; //繞過證書驗證,處理https請求 SSLContext sslcontext = createIgnoreVerifySSL(); // 設置協議http和https對應的處理socket鏈接工廠的對象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); //創建自定義的httpclient對象 CloseableHttpClient client = proxy("127.0.0.1", 8087).setConnectionManager(connManager).build(); // CloseableHttpClient client = HttpClients.createDefault(); //創建post方式請求對象 HttpPost httpPost = new HttpPost(url); //裝填參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //設置參數到請求對象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); System.out.println("請求地址:"+url); System.out.println("請求參數:"+nvps.toString()); //設置header信息 //指定報文頭【Content-type】、【User-Agent】 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //執行請求操作,并拿到結果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //獲取結果實體 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定編碼轉換結果實體為String類型 body = EntityUtils.toString(entity, encoding); } EntityUtils.consume(entity); //釋放鏈接 response.close(); return body; } ~~~ ? ? ?測試代碼如下: ~~~ public static void main(String[] args) throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException{ String url = "https://www.facebook.com/"; String body = send(url, null, "utf-8"); System.out.println("交易響應結果"); System.out.println(body); } ~~~ ? ? ?運行后,結果: ![](https://box.kancloud.cn/2016-02-18_56c53cb802de8.jpg) ? ? ?果然可以訪問成功了。 ? ? ?好了基本的教程就到這里,下篇我將其封裝的一個工具類,自認為相對于網上分享的封裝類要強大很多,敬請期待吧。
                  <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>

                              哎呀哎呀视频在线观看