为什么会丢失一个Cookie? [英] Why is one cookie missed?

查看:954
本文介绍了为什么会丢失一个Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除重定向导致的页面:我首先访问page1,然后通过 http-equiv =refresh重定向到page2。我在报废page2。第2页的内容基于某些Cookie page1集。我看到page1返回2个cookie,但是当我请求第2页(发送同样 CookieContainer 时,一个cookie丢失了)我的代码有什么问题?



谢谢:



第一
我创建一个 CookieContainer HttpWebRequest 并请求page1。




HttpWebRequest req =(HttpWebRequest)HttpWebRequest.Create(eQuery);

req.AllowAutoRedirect = true; //但它不会自动重新导向元刷新

req.CookieContainer = cookiesContainer;



这是我从访问page1获得的结果



HTTP / 1.1 200 OK

日期:Tue,2011年4月12日19:14:06 GMT

服务器:(...)< br>
Set-Cookie:NAME1 = VALUE1; path = /

到期日:Thu,19 Nov 1981 08:52:00 GMT

Cache-Control:no-存储,无高速缓存,必须重新生效,后检查= 0,预检查= 0

Pragma:no-cache

Set-Cookie:NAME2 = VALUE2; expires = 2011年4月13日星期三,2011年4月13日19:14:06 GMT

Content-Length:174

Keep-Alive:timeout = 5,max = 100

连接:保持活动

Content-Type:text / html

(...)



到目前为止,一切都很好,我得到两个cookie,并在容器中获得两个cookie对象。



然后我解析 值 meta http-equiv 用于下一个URL。并请求使用类似的代码,并使用相同的容器。但只发送一个cookie。这是HTTP发送的:




GET DETECTED_URL_IN_HTTP_EQUIV_REFRESH HTTP / 1.1

User-Agent: 。)

主机:example.com

Cookie:NAME1 = VALUE1



由于您看到Cookie NAME2缺失。为什么会这样?是两个饼干有关的差异(一个有路径和其他有到期日期)?
任何想法如何传递这两个cookie?



PS:我没有访问page1,所以我不能设置路径或过期饼干。

解决方案



<如果你没有在你的cookie上指定一个路径,它将默认为请求的路径。因此,如果您在此请求中收到了没有路径声明的Cookie:

  http://contoso.com/subfolder/test。 aspx 

浏览器只会在子文件夹/ 目录。要让浏览器为所有路径发送它,您需要在设置cookie时包括 path = /


I'm scrapping a page which is the result of a redirect: I visit page1 first, then it redirects to page2 via http-equiv="refresh". I'm scrapping page2. Content on page2 is based on some cookies page1 sets. I see page1 returns 2 cookies, but when I request page 2 (sending the same CookieContainer, one cookie is missing. What's wrong in my code?

Thank you:

First : I create a CookieContainer and an HttpWebRequest and request for page1.

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(eQuery);
req.AllowAutoRedirect = true; // but it doesn't autoredirects the meta-refresh
req.CookieContainer = cookiesContainer;

This is the result I get this from visiting page1

HTTP/1.1 200 OK
Date: Tue, 12 Apr 2011 19:14:06 GMT
Server: (...)
Set-Cookie: NAME1=VALUE1; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: NAME2=VALUE2; expires=Wed, 13-Apr-2011 19:14:06 GMT
Content-Length: 174
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
(...)

Everything is fine so far, I get two cookies are there and I get two cookie objects within the container.

Then I parse the "content" value of the meta http-equiv for the next url. And request it using a similar code and using the same container. But only one cookie is sent. Here is the HTTP sent:

GET DETECTED_URL_IN_HTTP_EQUIV_REFRESH HTTP/1.1
User-Agent: (...)
Host: example.com
Cookie: NAME1=VALUE1

As you see cookie NAME2 is missing. Why is that happening? is something related differences in the two cookies (one has path and other has expiration date)? Any idea how can I pass the two cookies?

PS: I don't have access to page1, so I can't set path, or expiration for cookies. I'm scrapping those pages.

Thank you.

解决方案

If you don't specify a path on your cookie it will default to the path it was requested on. So if you received a cookie on this request with no path declaration:

http://contoso.com/subfolder/test.aspx

The browser would only send back that cookie for more requests in the /subfolder/ directory. To have the browser send it back for all paths you need to include path=/ when setting the cookie.

这篇关于为什么会丢失一个Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆