【发布时间】: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
按照这些链接中的这些步骤进行操作
修复该问题后,在发布请求时会引发
线程“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。
请建议我解决此问题。
【问题讨论】: