使用Java访问https接口javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

jdk1.6,证书:SHA256+TLSv1.2
使用Java访问https://**************** 接口     控制台提示握手失败错误;javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
     解决办法:    
1.jdk版本问题,换用jdk1.7;
2.如不能更换jdk版本,引入 bcprov-jdk15on-157.jar 此jar包对jdk1.5-1.8都可支持;
public static String httpsRequsetForTLSv12(String host, String url) throws Exception {
        java.security.SecureRandom secureRandom = new java.security.SecureRandom();
        Socket socket = new Socket(java.net.InetAddress.getByName(host), 443);
        TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream(),secureRandom);
        DefaultTlsClient client = new DefaultTlsClient() {
            public TlsAuthentication getAuthentication() throws IOException {
                TlsAuthentication auth = new TlsAuthentication() {
                    public void notifyServerCertificate(org.bouncycastle.crypto.tls.Certificate serverCertificate) throws IOException {
                    }

                    public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
                        return null;
                    }
                };
                return auth;
            }
        };
        protocol.connect(client);
        java.io.OutputStream output = protocol.getOutputStream();
        output.write(("GET "+url+" HTTP/1.1\r\n").getBytes("UTF-8"));
        output.write(("Host: "+host+"\r\n").getBytes("UTF-8"));
        output.write("Connection: close\r\n".getBytes("UTF-8")); 
        output.write("\r\n".getBytes("UTF-8")); 
        output.flush();

        java.io.InputStream input = protocol.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
        String line;
        String result = null;
        while ((line = reader.readLine()) != null)
        {
        	result = line;
        	//System.out.println(line);
        }
        return result;
	}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值