[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.setRequestMethod("POST");// post
	// 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");
	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
	InputStream is = connection.getInputStream();
	BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	while ((strRead = reader.readLine()) != null) {
		// sbf.append("\r\n");
	String jsonStr = sbf.toString().replace("\"", "'");
	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!");
	SSLSessionContext sslsc = sc.getServerSessionContext();
	// Implement the X509TrustManager interface for bypassing authentication
	X509TrustManager trustManager = new X509TrustManager() {
		public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
				String paramString) throws CertificateException {

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

		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!");
	return sc;

Similar Posts: