Java:IllegalStateArgument 的 HttpURLConnection 问题:已连接

发布于 2024-10-24 15:14:26 字数 1831 浏览 1 评论 0原文

这个问题似乎很清楚发生了什么:我已经打开了一个连接,问题是我不知道为什么。

现在我只是测试我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-10-31 15:14:26

在写入请求正文之前,您需要设置 cookie(请求标头值)。发送请求正文的第一位后,您将无法再更改请求标头。

因此,更改

writeToOutput(con, content);
setCookies(con);

setCookies(con);
writeToOutput(con, content);

,这个问题应该消失。

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

writeToOutput(con, content);
setCookies(con);

to

setCookies(con);
writeToOutput(con, content);

and this problem should disappear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文