Httpclient 解決javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException

1.Httpclient jar 版本:httpclient-4.5.3.jar

2.GET請求拋出異常:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException

3.GET 方法解決異常, 修改GET方法如下:

public String doHttpClientGetRequestForSSL(String request) throws ClientProtocolException, IOException {
        CloseableHttpClient httpclient;
        BufferedReader in = null;
        String responseAsString = null;
        try {
            httpclient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build())).build();
            HttpUriRequest httpGet = new HttpGet(request);
            CloseableHttpResponse response = httpclient.execute(httpGet);
//            System.out.println(response.getStatusLine());
            responseAsString = EntityUtils.toString(response.getEntity());
//            System.out.println(responseAsString);
            response.close();
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return responseAsString;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章