赞
踩
目录
最近用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: []所以 要忽略校验和域名
- SSLContext sslContext = getSSLContext();
- Registry<ConnectionSocketFactory> socketFactoryRegistry =
- RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.INSTANCE)
- .register("https", new SSLConnectionSocketFactory(sslContext)).build();
- PoolingHttpClientConnectionManager mananger = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
- mananger.setMaxTotal(100);
- mananger.setDefaultMaxPerRoute(20);
- CloseableHttpClient httpclient = HttpClients
- .custom()
- .setDefaultRequestConfig(requestConfig)
- .setConnectionManager(mananger)
- .build();
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;
}
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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。