SSLException: Certificate verification failed
Original request method:
/**
* Post方式请求,返回Json数据
* @param requestUrl
* @param requestParams
* @return
*/
public static String sendRequestByPost(String requestUrl, Map<String, String> requestParams){
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection connection = null;
String response = null;
try {
Set<String> paramKeys = requestParams.keySet();
int i = 0;
String data = "";
for(String paramKey : paramKeys){
if(i == 0){
data = paramKey + "=" + requestParams.get(paramKey);
}else{
data = data + "&" + paramKey + "=" + requestParams.get(paramKey);
}
i++;
}
logger.info("Request url:"+requestUrl);
logger.info("Request data:"+data);
//创建连接
// Send the request
URL url = new URL(requestUrl);
connection = (HttpsURLConnection)url.openConnection();
TrustManager[] tm = {
ignoreCertificationTrustManger };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
connection.setSSLSocketFactory(ssf);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
connection.setRequestProperty("Charset", "UTF-8");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
//write parameters
writer.write(data);
writer.flush();
// 获取响应
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
//Output the response
logger.info(answer.toString());
response = answer.toString();
//断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
The replacement request method is:
/**
* Post方式请求,返回String数据 支持服务器jdk1.8
* @param requestUrl
* @param requestParams
* @return
*/
public static String sendRequestByPostForTLSv1(String requestUrl, Map<String, String> requestParams,Map<String, String> hearder){
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection connection = null;
String response = null;
try {
Set<String> paramKeys = requestParams.keySet();
int i = 0;
String data = "";
for(String paramKey : paramKeys){
if(i == 0){
data = paramKey + "=" + requestParams.get(paramKey);
}else{
data = data + "&" + paramKey + "=" + requestParams.get(paramKey);
}
i++;
}
logger.info("Request url:"+requestUrl);
logger.info("Request data:"+data);
//创建连接
// Send the request
URL url = new URL(requestUrl);
connection = (HttpsURLConnection)url.openConnection();
TrustManager[] tm = {
ignoreCertificationTrustManger };
SSLContext sslContext= SSLContext.getInstance("TLSv1.2");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
connection.setSSLSocketFactory(ssf);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
connection.setRequestProperty("Charset", "UTF-8");
connection.setDoOutput(true);
paramKeys = hearder.keySet();
for(String paramKey : paramKeys){
connection.setRequestProperty(paramKey, hearder.get(paramKey));
}
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
//write parameters
writer.write(data);
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
//Output the response
logger.info(answer.toString());
response = answer.toString();
//断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}