gpt4 book ai didi

java - IBM java POST API 抛出 SSL 握手异常

转载 作者:搜寻专家 更新时间:2023-10-31 19:36:37 24 4
gpt4 key购买 nike

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

Exception in thread "main" java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class 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/

修复该问题后,在发布请求时抛出

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: 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。

请建议我解决这个问题。

最佳答案

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

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

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

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

关于java - IBM java POST API 抛出 SSL 握手异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52723832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com