要信任所有的证书,并关闭校验
public String doPost(String method, Long curTime, String data) throws Exception {
String nonce = Const.getStr(Math.random());
String checkSum = encode("sha1", getConfigByKey("sh2appSecret")+nonce+curTime);
String params = "?appId="+getConfigByKey("sh2Appid")+"&nonce="+nonce+"&curTime="+curTime+"&checkSum="+checkSum;
HttpPost httpPost = new HttpPost(getConfigByKey("sh2Url")+method+getConfigByKey("sh2ParkingId")+params);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(2000)
.setConnectTimeout(5000)
.setSocketTimeout(20000).build();
//信任所有证书,关闭主机名校验
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
// CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String sign=encode("md5", getConfigByKey("sh2appSecret")+"|"+data);
httpPost.setHeader("sign", sign);
StringEntity entity = new StringEntity(data, "UTF-8");
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
//LOG.info("SH2--" + method + ",sign:" + sign+",data="+data);
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
LOG.info("SH2--" + method + ",状态码是:" + state);
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}