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; }
Similar Posts:
- [Solved] remote calling three-party interface error: javax.net.ssl.SSLHandshakeException
- [Solved] javax.net.ssl.SSLException: Received fatal alert: protocol_version
- [Solved] No converter found for return value of type: class org.json.JSONObject
- HttpURLConnection Cannot write output after reading input.
- [Solved] java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”
- [Solved] Json.Net – Error getting value from ‘ScopeId’ on ‘System.Net.IPAddress’
- [Solved] Json.Net Error: Error getting value from ‘ScopeId’ on ‘System.Net.IPAddress’
- [Solved] Zookeeper reads data error: Exception in thread “main” org.I0Itec.zkclient.exception.ZkMarshallingError…
- [Solved] net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.
- Server returned HTTP response code: 500 for URL xxxxxxxxxxxxx