繁体   English   中英

Double Set-Cookie:http响应中的PHPSESSID

[英]Double Set-Cookie: PHPSESSID in http response

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

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

Set-Cookie:PHPSESSID = 3fvr31tb3c1iplpi3vqpvloar3; 路径= /; domain = .bursatransport.com

Set-Cookie:PHPSESSID = eanaj1d9egd73uiome0jtsed43; 路径= /; domain = .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();

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

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

响应包含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:使用POST登录时处理cookie

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM