gpt4 book ai didi

java.lang.IllegalStateException : Already connected for setRequestProperty method : Java 错误

转载 作者:行者123 更新时间:2023-11-30 10:41:33 36 4
gpt4 key购买 nike

我创建了一个类,我正在创建一个 HttpURLConnection对象并将该对象传递给另一个方法,如果它满足以下条件:

在这门课中,我通过了 username , passwordsenderId使用 proxy 连接到某个 URL当连接不为空时。

下面是Java类的完整代码:

public class SMSGenerator {
public static HttpURLConnection sendSingleSMS(HttpURLConnection lconnection, String username,
String password, String senderId,
String mobileNo, String message) {
try {
String smsservicetype = "singlemsg"; // For single message.
String query = "username=" + URLEncoder.encode(username)
+ "&password=" + URLEncoder.encode(password)
+ "&smsservicetype=" + URLEncoder.encode(smsservicetype)
+ "&content=" + URLEncoder.encode(message) + "&mobileno="
+ URLEncoder.encode(mobileNo) + "&senderid="
+ URLEncoder.encode(senderId);

lconnection.setRequestProperty("Content-length", String.valueOf(query.length()));
lconnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
lconnection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");

// open up the output stream of the connection
DataOutputStream output = new DataOutputStream(lconnection.getOutputStream());

// write out the data
int queryLength = query.length();
output.writeBytes(query);
// output.close();

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

// read in each character until end-of-stream is detected
for (int c = input.read(); c != -1; c = input.read()) {
System.out.print((char) c);
}
input.close();
} catch (Exception e) {
System.out.println("Something bad just happened.");
e.printStackTrace();
}
return lconnection;
}
public static boolean sendSMS(String mobilenumber, String msg) throws MalformedURLException, IOException {
boolean flag = false;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxx", xx));
URL surl = new URL("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
HttpURLConnection connection = null;
connection = (HttpURLConnection) surl.openConnection(proxy);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
HttpURLConnection.setFollowRedirects(true);
System.out.println("Response code " + connection.getResponseMessage());
System.out.println("code : " + connection.getResponseCode() + " " + HttpURLConnection.HTTP_NOT_FOUND);
if ("OK".equals(connection.getResponseMessage())) {
System.out.println("in");
SMSGenerator.sendSingleSMS(connection, "username", "password", "SMS", mobilenumber, msg);
// System.out.println("Sent successful SMS");
flag = true;

} else if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Hiiii");
} else if (connection.getResponseCode() == HttpURLConnection.HTTP_BAD_GATEWAY) {
System.out.println("Hi 1");
} else if (connection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
System.out.println("Hi 2");
}
// System.out.println("");
return flag;
}
public static void main(String[] args) throws IOException {
new SMSGenerator().sendSMS("xxxxxxxxxx", "ManojYest1");
}
}

这段代码的问题在于,如果我检查 if条件为:connection != null然后它建立连接并正确调用该方法。但是,如果我用 responseCode 检查条件作为connection.getResponseCode() == HttpURLConnection.HTTP_OK然后出现以下错误:

java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3013)

我该如何解决这个错误?

最佳答案

因为不应该那样使用连接。您正在尝试执行以下操作:

"Open" Connection
Get Response
|-> Connect (if not already connected)
|-> Read response data
setRequestProperty
|-> Check connected state - Oops! Already connected!

这不是您应该使用 UrlConnection 的方式。相反,它希望您执行以下操作:

"Open" Connection
setRequestProperty
|-> Check connected state - Not Connected
getOutputStream
|-> Connect
|-> Create and return output stream
Write request to output stream
Get Response
|-> Connect (if not already connected)
|-> Send (empty) request
|-> Read response data

您是否需要知道在发送数据之前 会收到 OK 响应?或者您只是想验证您是否已成功连接到端点?

openConnection 不会导致连接对象实际连接 到目标 URL...

关于java.lang.IllegalStateException : Already connected for setRequestProperty method : Java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461985/

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