gpt4 book ai didi

java - jsp HTTPS POST 请求

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:05 27 4
gpt4 key购买 nike

我正在尝试从 jsp 向 Opera 服务器发送 HTTPS Post 请求。但我不能。我发现了很多 HTTP Post 请求示例,但没有找到 HTTPS。我现在收到一条错误消息..

java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection 

我对此代码的导入如下

java.security.Security, com.sun.net.ssl.

我的代码如下

try{     
String data = URLEncoder.encode("AccountID", "UTF-8") + "=" + URLEncoder.encode(accountid, "UTF-8");
data += "&" + URLEncoder.encode("CallerTransactionID", "UTF-8") + "=" + URLEncoder.encode(callertransactionid, "UTF-8");
data += "&" + URLEncoder.encode("CurrentTime", "UTF-8") + "=" + URLEncoder.encode(currenttime, "UTF-8");
data += "&" + URLEncoder.encode("ErrorURL", "UTF-8") + "=" + URLEncoder.encode(errorurl, "UTF-8");
//data += "&" + URLEncoder.encode("FrameURL", "UTF-8") + "=" + URLEncoder.encode(frameurl, "UTF-8");

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
URL opxurl = new URL("https://opx-test.opera.com/opx/2.0/OPXPaymentEnable");
HttpsURLConnection connopx = (HttpsURLConnection) opxurl.openConnection();
connopx.setDoInput(true);
connopx.setDoOutput(true);
connopx.setRequestMethod("POST");
connopx.setFollowRedirects(true);
connopx.setRequestProperty("Content-length",String.valueOf(data.length()));
connopx.setRequestProperty("Content-Type","application/x-www- form-urlencoded");

// open up the output stream of the connection
DataOutputStream output = new DataOutputStream(connopx.getOutputStream());
// write out the data
int dataLength = data.length();
output.writeBytes(data);
//output.flush();

System.out.println("HOly Portal ResponseDesc Code:"+connopx.getResponseCode()+" : "+connopx.getResponseMessage());

// get ready to read the response from the cgi script
DataInputStream input = new DataInputStream( connopx.getInputStream() );

// read in each character until end-of-stream is detected
for( int c = input.read(); c != -1; c = input.read() )
System.out.print("HolyPortal respnse: "+ (char)c );

input.close();

最佳答案

完成所有 connopx 设置后,您必须调用

connopx.connect();

我使用这个导入,它在我的代码中工作:

import javax.net.ssl.HttpsURLConnection;

我没有上面的两行URL opxurl =

看看这个 answer with similar issue还有。

编辑:

为了避免错误 500,请删除“application/x-www-form-urlencoded”中的空格,并在 output.writeBytes(data); 之后添加这两行:

output.flush();
output.close();

然后它应该可以工作。

关于java - jsp HTTPS POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16495111/

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