当前位置:   article > 正文

用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn‘t match any of the subject alternative_certificate for doesn't match

certificate for doesn't match

目录

1:HttpClient 的https证书验证请求问题

2: post请求示例

3: get请求示例


 

1:HttpClient 的https证书验证请求问题

最近用http 推送,用apache HttpClient做https的请求(之前用阿里云的证书都是没问题的,这次是另一个开发者 他自签证书未申请颁发 域名也没备案),代码上到服务器端就报javax.net.ssl.SSLException: Certificate for <域名> doesn't match any of the subject alternative names: [域名],

 

仔细翻了一下文档发现是HttpClient 4.4.1版本的bug,试了很多解决方案,最后在stackoverflow上面找到了正解,链接如下:link,根本的解决还是要从调用HttpClient的代码入手,我把关键部分截图出来分享给掉进这个坑的朋友们:

单独处理下信任所有证书,关闭主机名校验,就能通过验证了 



补充:

看网上好多采取,有人可以,但是我这种情况是(毕竟自签为备案申请的)然后就会提示javax.net.ssl.SSLPeerUnverifiedException: Certificate for <singfleet.vip-design.net> doesn't match any of the subject alternative names: []所以 要忽略校验和域名

  1. SSLContext sslContext = getSSLContext();
  2. Registry<ConnectionSocketFactory> socketFactoryRegistry =
  3. RegistryBuilder.<ConnectionSocketFactory>create()
  4. .register("http", PlainConnectionSocketFactory.INSTANCE)
  5. .register("https", new SSLConnectionSocketFactory(sslContext)).build();
  6. PoolingHttpClientConnectionManager mananger = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
  7. mananger.setMaxTotal(100);
  8. mananger.setDefaultMaxPerRoute(20);
  9. CloseableHttpClient httpclient = HttpClients
  10. .custom()
  11. .setDefaultRequestConfig(requestConfig)
  12. .setConnectionManager(mananger)
  13. .build();

2: post请求示例

 

    public static RestOrderInfo postOrderInfo(String url,String json) throws ClientProtocolException, IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        HttpPost http= new HttpPost(url);  
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000) //与服务器连接超时时间(连接目标超时):httpclient会创建一个异步线程用以创建socket连接,此处设置该socket的连接超时时间
                .setConnectionRequestTimeout(1000) //从连接池中获取连接的超时时间   
                .setSocketTimeout(5000).build(); //设置0有危险;  socket读数据超时(等待响应超时)时间:从服务器获取响应数据的超时时间  ps:A timeout value of zero is interpreted as an infinite timeout.

        http.setConfig(requestConfig); 
        HttpEntity inEntity =EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build();
        http.setEntity(inEntity);
        CloseableHttpResponse response = httpclient.execute(http);  
        logger.info("--return:" + response.getStatusLine());
        HttpEntity entity = response.getEntity();//得到请求结果  
        StatusLine status = response.getStatusLine();
        RestOrderInfo rest = null;
        if (status.getStatusCode() == 200){
            String resultJson =  EntityUtils.toString(entity,"UTF-8");
            rest = JSON.parseObject(resultJson, RestOrderInfo.class);
        }
        response.close();
        return rest;
    }

3: get请求示例

public static boolean get(String url) throws ClientProtocolException, IOException {
   CloseableHttpClient httpclient = HttpClients.createDefault();  
   HttpGet http= new HttpGet(url);  
   RequestConfig requestConfig = RequestConfig.custom()  
           .setConnectTimeout(5000).setConnectionRequestTimeout(1000)  
           .setSocketTimeout(5000).build(); 
   http.addHeader("Stage", "release");
   http.addHeader("Timestamp", String.valueOf(System.currentTimeMillis()));
   http.addHeader("Content-Type", "application/json");
   http.setConfig(requestConfig);  
   CloseableHttpResponse response = httpclient.execute(http);  
   System.err.println("GET|sync_station_evshare|" + response.getStatusLine());//得到请求结果  
   StatusLine status = response.getStatusLine();
   response.close();
   return status.getStatusCode()==200;
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/321278
推荐阅读
相关标签
  

闽ICP备14008679号