与HTTPS网址建立连接时出错

时间:2018-04-03 10:34:44

标签: ssl https truststore

我尝试建立https连接。我通过以下代码加载了trustStore和密码:

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
String path = "/path to trust store";
String password = "password of trust store";
KeyStore keyStore = KeyStore.getInstance("p12");
InputStream inputStream = new FileInputStream(path);
keyStore.load(inputStream,password.toCharArray());
httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial((TrustStrategy) keyStore).build()));
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpGet request = new HttpGet(this.backendUrl + url);
request.addHeader("Accept", "application/json");
   String responseString = null;
        try {
            HttpResponse response = httpClient.execute(request);
            if (logger.isDebugEnabled()) {
                logger.debug("Request successful for " + url);
            }
            responseString = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (IllegalStateException e) {
            logger.error("The response is empty ");
        } catch (NullPointerException e) {
            logger.error("Bad request to the URL");
        } catch (IOException e) {
            logger.error("mke");
        }

我在浏览器中收到错误:

The character encoding of the plain text document was not declared. 
The document will render with garbled text in some browser configurations 
if the document contains characters from outside the US-ASCII range.
The character encoding of the file needs to be declared in the 
transfer protocol or file needs to use a byte order 
mark as an encoding signature.

0 个答案:

没有答案