Tag Archives: Exception in thread “main” javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

[Solved] Exception in thread “main” javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

The project needs to use java to call the third-party HTTPS interface. After debugging many times, it always reports javax.net.ssl.sslhandshakeexception: received fatal Alert: handshake_Failure error, Huangtian pays off his hard work. Finally, the debugging is successful. I don’t talk much nonsense and go directly to the code (my code is relatively complete, which is also convenient for me to check how to use it in the future);

public static String signature(String comeStr) throws Exception {
	SSLContext sc = createIgnoreVerifySSL();
	String serverURL = "https://xxx";
	StringBuffer sbf = new StringBuffer();
	String strRead = null;
	URL url = new URL(serverURL);
	HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
	connection.setSSLSocketFactory(sc.getSocketFactory());
	connection.setRequestMethod("POST");// post
	connection.setDoInput(true);
	connection.setDoOutput(true);
	// The parameters inside the header are set here connection.setRequestProperty("Jian, "value");
	connection.setRequestProperty("Accept", "application/json");// set the format of the received data
	connection.setRequestProperty("Content-Type", "application/json");
	connection.connect();
	OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
	// The body parameter is put into the JSONObject here
	JSONObject outParm = new JSONObject(jsonBuild());//need to convert the json data
	writer.write(outParm.toString());
	writer.flush();
	InputStream is = connection.getInputStream();
	BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	while ((strRead = reader.readLine()) != null) {
		sbf.append(strRead);
		// sbf.append("\r\n");
	}
	String jsonStr = sbf.toString().replace("\"", "'");
	reader.close();
	connection.disconnect();
	JSONObject inParm = new JSONObject(jsonStr);
	String results = (String) inParm.get("pdf");
	return results;
}
/**
 * Get SSL socket object Key point: set the version of tls protocol
 * @return
 */
public static SSLContext createIgnoreVerifySSL() {
	SSLContext sc = null;
	try {
          //Draw the point: here is extremely important need to correspond to each other's TLS version, version is not much online search a search, put in one by one to try (most of the use of TLSv1.2 version)
		sc = SSLContext.getInstance("TLSv1.2");// specify the TLS version
	} catch (NoSuchAlgorithmException e) {
		System.out.println("Failed to create a socket!");
		e.printStackTrace();
	}
	SSLSessionContext sslsc = sc.getServerSessionContext();
	sslsc.setSessionTimeout(0);
	// Implement the X509TrustManager interface for bypassing authentication
	X509TrustManager trustManager = new X509TrustManager() {
		@Override
		public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
				String paramString) throws CertificateException {
		}

		@Override
		public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
				String paramString) throws CertificateException {
		}

		@Override
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	};
	try {
		sc.init(null, new TrustManager[] { trustManager }, null);
	} catch (KeyManagementException e) {
		System.out.println("Failed to initialize the socket!");
		e.printStackTrace();
	}
	return sc;
}