java 使用httpClient 进行http请求报错:
java.lang.AssertionError: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
后添加如下方法:跳过ssl验证
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
post请求:
public static String doHttpsPost(String url, Map<String, Object> paramMap,String proxyIp,int proxyPort,String proxyScheme,String... authori) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
String result = "";
// 创建httpClient实例
httpClient = createSSLClientDefault();
// 创建httpPost远程连接实例
HttpPost httpPost = new HttpPost(url);
if(authori !=null && authori.length > 0 && StringUtils.isNotEmpty(authori[0])) {
httpPost.addHeader("token", authori[0]);
}
else{
httpPost.addHeader("token", "");
}
// 配置请求参数实例
HttpHost proxy = new HttpHost(proxyIp, proxyPort, proxyScheme);
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxy)//
.setConnectTimeout(35000)// 设置连接主机服务超时时间
.setConnectionRequestTimeout(35000)// 设置连接请求超时时间
.setSocketTimeout(60000)// 设置读取数据连接超时时间
.build();
// 为httpPost实例设置配置
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
//httpPost.addHeader("Proxy-Connection", "keep-alive");
// 为httpPost设置封装好的请求参数
if(paramMap != null && paramMap.size() > 0) {
StringEntity entityRequest = new StringEntity(JSONObject.fromObject(paramMap).toString(), Charset.forName("UTF-8"));
entityRequest.setContentType("application/json");
httpPost.setEntity(entityRequest);
}
try {
// httpClient对象执行post请求,并返回响应参数对象
httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK || statusCode == 201 || statusCode == 401) {
// 从响应对象中获取响应内容
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity);
}
else{
Assert.assertTrue(false,"接口调用失败,返回code=" + statusCode);
}
} catch (ClientProtocolException e) {
Assert.assertTrue(false,e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Assert.assertTrue(false,e.getMessage());
e.printStackTrace();
} finally {
// 关闭资源
if (null != httpResponse) {
try {
httpResponse.close();
} catch (IOException e) {
Assert.assertTrue(false,e.getMessage());
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
Assert.assertTrue(false,e.getMessage());
e.printStackTrace();
}
}
}
return result;
}