我们在请求第三方接口时,有时会出现ssl的校验,下面我们用Java为例:如何去忽略调ssl校验
- 用以下方式构建httpPost
public static String doPostBaiRong(String url, String str, String charset) {
HttpPost httpPost = null;
String result = null;
final SSLConnectionSocketFactory sslsf;
try {
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault(),
NoopHostnameVerifier.INSTANCE);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new PlainConnectionSocketFactory())
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(100);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setConnectionManager(cm)
.build();
try {
//httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
//httpPost.addHeader("Content-Type", "application/x-www-form-urlencode");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000 * 3).build();
httpPost.setConfig(requestConfig);
StringEntity se = new StringEntity(str, charset);
se.setContentType("text/json");
se.setContentEncoding("UTF-8");
httpPost.setEntity(se);
/*HttpResponse response = httpClient.execute(httpPost);*/
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
这种方式可以请求成功