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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】 ? ? ?在上篇文章《[HttpClient配置ssl實現https簡單示例——繞過證書驗證](http://blog.csdn.net/xiaoxian8023/article/details/49865335)》中簡單分享了一下如何繞過證書驗證。如果你想用httpclient訪問一個網站,但是對方的證書沒有通過ca認證或者其他問題導致證書不被信任,比如12306的證書就是這樣的。所以對于這樣的情況,你只能是選擇繞過證書驗證的方案了。 ? ? ?但是,如果是自己用jdk或者其他工具生成的證書,還是希望用其他方式認證自簽名的證書,這篇文章就來分享一下如何設置信任自簽名的證書。當然你也可以參考[官網示例](http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientCustomSSL.java)中。 ? ? ?要想信任自簽名的證書,必須得知道密鑰庫的路徑及密鑰庫的密碼。然后加載到程序來才可以。具體代碼如下: ~~~ /** * 設置信任自簽名證書 * * @param keyStorePath 密鑰庫路徑 * @param keyStorepass 密鑰庫密碼 * @return */ public static SSLContext custom(String keyStorePath, String keyStorepass){ SSLContext sc = null; FileInputStream instream = null; KeyStore trustStore = null; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); instream = new FileInputStream(new File(keyStorePath)); trustStore.load(instream, keyStorepass.toCharArray()); // 相信自己的CA和所有自簽名的證書 sc = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); } catch (KeyStoreException | NoSuchAlgorithmException| CertificateException | IOException | KeyManagementException e) { e.printStackTrace(); } finally { try { instream.close(); } catch (IOException e) { } } return sc; } ~~~ ? ? ?然后修改原來的send方法: ~~~ /** * 模擬請求 * * @param url 資源地址 * @param map 參數列表 * @param encoding 編碼 * @return * @throws ParseException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException * @throws ClientProtocolException */ public static String send(String url, Map<String,String> map,String encoding) throws ClientProtocolException, IOException { String body = ""; //tomcat是我自己的密鑰庫的密碼,你可以替換成自己的 //如果密碼為空,則用"nopassword"代替 SSLContext sslcontext = custom("D:\\keys\\wsriakey", "tomcat"); // 設置協議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 = HttpClients.custom().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://sso.tgb.com:8443/cas/login"; String body = send(url, null, "utf-8"); System.out.println("交易響應結果長度:"+body.length()); System.out.println("-----------------------------------"); url = "https://kyfw.12306.cn/otn/"; body = send(url, null, "utf-8"); System.out.println("交易響應結果長度:"+body.length()); } ~~~ ? ? ?測試結果: ![](https://box.kancloud.cn/2016-02-18_56c53cb7ae964.jpg) ? ? ?從結果中,我們很清楚的看到,使用自簽名的證書,訪問自簽名的網站可以正常訪問,訪問12306則會失敗。所以自簽名的也只能用于自定義密鑰和證書的情況下使用。而12306這種情況還是要用上一篇提到的“[繞過證書驗證](http://blog.csdn.net/xiaoxian8023/article/details/49865335)”方案。
                  <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>

                              哎呀哎呀视频在线观看