javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

问题描述:在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();
            }
        }
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值