Java:IllegalStateArgument 的 HttpURLConnection 问题:已连接
这个问题似乎很清楚发生了什么:我已经打开了一个连接,问题是我不知道为什么。
现在我只是测试我的 http 登录和退出。
登录:
HttpURLConnection con = openConnection(URLGenerator.getLoginURL(), true, true,"POST");
String content = ...;
writeToOutput(con, content);
con.connect();
String cookieVal = con.getHeaderField("Set-Cookie");
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}
con.disconnect();
return con.getResponseCode();
注销:
HttpURLConnection con = openConnection(URLGenerator.getLogoutURL(), true, true,"GET");
String content = ...;
writeToOutput(con, content);
setCookies(con);
con.connect();
con.disconnect();
return con.getResponseCode();
对于代码爱好者来说,函数 OpenConnection (因为我知道人们会问的第一件事是“这个函数在哪里”
public static final HttpURLConnection openConnection(URL url, boolean in, boolean out,String requestMethode) throws IOException{
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
con.setDoInput(in);
con.setDoOutput (out);
if(requestMethode == null){
requestMethode = "GET";
}
con.setRequestMethod(requestMethode);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
return con;
}
这是触发 java.lang.IllegalStateException: Already connect< /strong>
public static final void setCookies(HttpURLConnection con){
if(sessionId != null)
{
con.setRequestProperty("Cookie", sessionId);
}
}
我不明白的是为什么连接仍然打开,
但它不起作用,
我认为主要是设置一个 HttpURLConncetion 对象并连接以执行请求,接收。结果 有
什么想法吗?
谢谢杰森,
This question seems clear what is happening: I already have a connection open, the problem is I don't know why.
Right now I'm just testing my http login and out.
Login:
HttpURLConnection con = openConnection(URLGenerator.getLoginURL(), true, true,"POST");
String content = ...;
writeToOutput(con, content);
con.connect();
String cookieVal = con.getHeaderField("Set-Cookie");
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}
con.disconnect();
return con.getResponseCode();
Logout:
HttpURLConnection con = openConnection(URLGenerator.getLogoutURL(), true, true,"GET");
String content = ...;
writeToOutput(con, content);
setCookies(con);
con.connect();
con.disconnect();
return con.getResponseCode();
and for the code lovers the function OpenConnection (because I know the first thing people are going to ask is "where is this function"
public static final HttpURLConnection openConnection(URL url, boolean in, boolean out,String requestMethode) throws IOException{
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
con.setDoInput(in);
con.setDoOutput (out);
if(requestMethode == null){
requestMethode = "GET";
}
con.setRequestMethod(requestMethode);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
return con;
}
This is the function that triggers the java.lang.IllegalStateException: Already connected
public static final void setCookies(HttpURLConnection con){
if(sessionId != null)
{
con.setRequestProperty("Cookie", sessionId);
}
}
What I don't get is why the connection is still open.
I even tried to call disconnect but it doesn't work.
I thought the principal was to set up a HttpURLConncetion object and do connect to execute the request, receive the result and that terminated the connection.
Any ideas?
Thanks Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在写入请求正文之前,您需要设置 cookie(请求标头值)。发送请求正文的第一位后,您将无法再更改请求标头。
因此,更改
为
,这个问题应该消失。
You need to set cookies (request header values) before you write to the request body. You can't change the request headers anymore when the first bit of the request body is been sent.
So, change
to
and this problem should disappear.