使用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;
	}





你可能感兴趣的:(java)