问题描述:在jdk1.7环境中使用HttpURLConnection发送https请求时,异常了
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
同样代码在jdk1.8环境中正常执行。
源码:
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
public static
void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
// 请求头
Map<String, String> headers = new HashMap<String, String>();
headers.put("AppCode", "1234567890");
// url
String url = "https://xxx.xxx.com/abc";
System.out.println(url);
BufferedReader in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(10000);
conn.setRequestMethod("POST");
//request header
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
// request body
Map<String, Boolean> methods = new HashMap<>();
methods.put("POST", true);
methods.put("PUT", true);
methods.put("PATCH", true);
Boolean hasBody = methods.get(method);
if (hasBody != null) {
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.flush();
out.close();
}
int rescode = conn.getResponseCode();
System.err.println("http status code ==> " + rescode);
String result = "";
try {
//定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
InputStream inputStream = conn.getErrorStream();
result = getStringFromInputStream(inputStream);
}
System.out.println(result);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
解决方案:
在jdk1.7中使用HttpURLConnection发送https请求,在建立连接前添加以下代码:
//https请求jdk1.7添加以下几行代码
SSLContext ctx = SSLContext.getInstance("TLSv1.2");
ctx.init(null, null, null);
SSLContext.setDefault(ctx); //将你所要使用的TLS版本设为默认
Security.setProperty("ssl.SocketFactory.provider", "");
Security.setProperty("ssl.ServerSocketFactory.provider", "");
System.setProperty("https.protocols", "TLSv1.2");
详情 :
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLContext;
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
// 请求头
Map<String, String> headers = new HashMap<String, String>();
headers.put("AppCode", "1234567890");
// url
String url = "https://xxx.xxx.com/abc";
System.out.println(url);
BufferedReader in = null;
try {
URL realUrl = new URL(url);
//https请求jdk1.7添加以下几行代码
SSLContext ctx = SSLContext.getInstance("TLSv1.2");
ctx.init(null, null, null);
SSLContext.setDefault(ctx); //将你所要使用的TLS版本设为默认
Security.setProperty("ssl.SocketFactory.provider", "");
Security.setProperty("ssl.ServerSocketFactory.provider", "");
System.setProperty("https.protocols", "TLSv1.2");
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(10000);
conn.setRequestMethod("POST");
//request header
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
// request body
Map<String, Boolean> methods = new HashMap<>();
methods.put("POST", true);
methods.put("PUT", true);
methods.put("PATCH", true);
Boolean hasBody = methods.get(method);
if (hasBody != null) {
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.flush();
out.close();
}
int rescode = conn.getResponseCode();
System.err.println("http status code ==> " + rescode);
String result = "";
try {
//定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
InputStream inputStream = conn.getErrorStream();
result = getStringFromInputStream(inputStream);
}
System.out.println(result);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}