【问题标题】:IBM java POST API Throws an SSL HandShake ExceptionIBM java POST API 引发 SSL 握手异常
【发布时间】:2019-03-14 09:32:31
【问题描述】:

我尝试使用 IBM Java(1.6) 创建 API 休息调用后,它是 Websphere 服务器 7(应用程序服务器)的开箱即用。在建立连接时它会抛出

线程“main”java.net.SocketException 中的异常:java.lang.ClassNotFoundException:找不到指定的类 com.ibm.websphere.ssl.protocol.SSLSocketFactory

按照这些链接中的这些步骤进行操作

https://www.link-intersystems.com/blog/2014/07/20/how-to-fix-java-lang-classnotfoundexception-com-ibm-websphere-ssl-protocol-sslsocketfactory-in-eclipse/

修复该问题后,在发布请求时会引发

线程“main”javax.net.ssl.SSLHandshakeException 中的异常:收到致命警报:handshake_failure

同样的代码在 Oracle Java(1.6) 上也能正常工作

下面的代码是我试过的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.Security;

public class POST {

     public static void main(String[] args){


        try {
            POSTRequest();
        } catch (IOException e) {
            System.out.println("Error" + e);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
    public static void POSTRequest() throws IOException {
        Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
        Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
        System.out.println("Begin Post Request");
        final String POST_PARAMS = "{\n" + "\"userId\": 151,\r\n" +
            "    \"id\": 101,\r\n" +
            "    \"title\": \"VM Title\",\r\n" +
            "    \"body\": \"Test Body\"" + "\n}";
        System.out.println(POST_PARAMS);
        URL obj = new URL("https://jsonplaceholder.typicode.com/posts");
        HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
        postConnection.setRequestMethod("POST");
        postConnection.setRequestProperty("userId", "a1bcdefgh");
        postConnection.setRequestProperty("Content-Type", "application/json");
        postConnection.setDoOutput(true);
        OutputStream os = postConnection.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        int responseCode = postConnection.getResponseCode();
        System.out.println("POST Response Code :  " + responseCode);
        System.out.println("POST Response Message : " + postConnection.getResponseMessage());
        if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                postConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in .readLine()) != null) {
                response.append(inputLine);
            } in .close();
            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST NOT WORKED");
        }
    }




}

Post_API.txt 显示 Post_API.txt。

请建议我解决此问题。

【问题讨论】:

    标签: java rest api websphere


    【解决方案1】:

    您可以尝试在提交给 Web 服务的请求中设置协议。

    System.setProperty(“https.protocols”, “TLSv1,TLSv1.1,TLSv1.2”);
    

    另外,需要注意的是修订包的版本和 WebSphere 中使用的 Java 版本。 WebSphere 使用与安装捆绑在一起的自己的 Java 版本。

    如果 WebSphere 安装的 Java 版本较低,您可能需要升级它。

    【讨论】:

      【解决方案2】:

      handshake_failure 通常与不匹配的 SSL/TLS 协议或密码套件有关。

      但是,在这种情况下,如果您查看 Qualys SSL tester for this site,返回的 4 个服务器表明 Java 6 将由于 SNI 不受支持而失败。

      所以我猜 Oracle Java 6 有一些不同的默认 SSL 设置,或者设置了覆盖默认行为的设置,或者 WebSphere Java 6 可能是 Java 6 的较旧修复级别,或者两个 Java 的序列TLS 级别和密码套件的协商是不同的。

      还要注意WebSphere 7 is now out of supportJava 6 也是如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 2015-01-24
        • 1970-01-01
        相关资源
        最近更新 更多