【问题标题】:Double Set-Cookie: PHPSESSID in http responseDouble Set-Cookie:http 响应中的 PHPSESSID
【发布时间】:2013-08-20 20:45:23
【问题描述】:

我正在编写一个可以在网站上自动完成表单的应用程序;(在 Java 中)

用户必须登录才能执行此操作,这就是问题出现的地方: 这是对登录请求的响应的一部分:

设置 Cookie:PHPSESSID=3fvr31tb3c1iplpi3vqpvloar3;路径=/;域名=.bursatransport.com

设置 Cookie:PHPSESSID=eanaj1d9egd73uiome0jtsed43;路径=/;域名=.bursatransport.com

据我测试,最后一个是正确的(我通过更改浏览器中的PHPSESSID cookie进行测试)

我的应用程序保留了第一个 cookie。结果,在提交表单时,它的行为就好像用户没有登录一样。 有时它保留了最后一个cookie,但它没有成功提交表单(和以前一样)。

这是我的登录代码:

String query = String
            .format("returnTo=/&Login[username]=%s&Login[password]=%s&Login[rememberMe]=0&yt4=",
                    URLEncoder.encode(name, charset),
                    URLEncoder.encode(password, charset));
    CookieManager manager = new CookieManager();
    manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(manager);
    URLConnection mycon = new URL(url).openConnection();
    mycon.setDoOutput(true);
    mycon.setRequestProperty("Accept-Language", "ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4");
    mycon.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    mycon.setRequestProperty("Accept-Charset", charset);
    mycon.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=" + charset);
    OutputStream output = null;
    output = mycon.getOutputStream();
    output.write(query.getBytes(charset));
    mycon.getContent();

这肯定不是服务器问题,因为它正确响应了浏览器请求(我正在用 fiddler 监听它们)

【问题讨论】:

    标签: java http cookies urlconnection


    【解决方案1】:

    我解决了这个问题(即使我仍然不知道它的根源)。

    响应包含 2 个“Set-Cookie”标头,因为(这不是您最一致的原因)我的请求不包含 PHPSESSID cookie; 所以我更改了代码,以便它首先获取登录页面(没有登录数据)。 对这个请求集的响应是一个 PHPSESSID cookie(但我没有登录)

    然后我发送我的登录请求(现在包含一个 PHPSESSID cookie),然后,它可以工作了。

    代码如下:

        CookieManager manager = new CookieManager();
        CookieHandler.setDefault(manager);
        URLConnection mycon = new URL(url).openConnection();
        mycon.getContent();
        String query = String
                .format("Login[username]=%s&Login[password]=%s&Login[rememberMe]=0&yt4=",
                        URLEncoder.encode(name, charset),
                        URLEncoder.encode(password, charset));
        mycon = new URL(url).openConnection();
        mycon.setDoOutput(true);
        mycon.setRequestProperty("Accept-Language", "ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4");
        mycon.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        mycon.setRequestProperty("Accept-Charset", charset);
        mycon.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=" + charset);
        OutputStream output = null;
        output = mycon.getOutputStream();
        output.write(query.getBytes(charset));
        output.close();
        mycon.getContent();
        mycon.getInputStream().close();
    

    这篇文章“让我大开眼界”: Java: Handling cookies when logging in with POST

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2018-03-16
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      相关资源
      最近更新 更多