httpwebrequest/webclient不保留跨域的cookie [英] httpwebrequest / webclient not preserving cookies across domain

查看:65
本文介绍了httpwebrequest/webclient不保留跨域的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我有一个应用程序,它使用带有cookie容器的httpwebrequest(最初是启用cookie的webclient)登录网站,开始时一切正常.

基本上,我会收到cookie并检查它是否存在于cookieContainer中,只要我停留在该页面上,它就会将其发送回网站.但是,每当我尝试导航到同一域(甚至在同一文件夹中)的任何其他页面时,就不再发送Cookie.

我通常使用.NET 2.0进行构建,但也尝试使用3.5并发现了完全相同的行为.

这是附加了cookiecontainer的httpwebrequest的预期行为,还是我的代码中缺少某些内容?

request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "text/html";
request.AllowAutoRedirect = true;
request.CookieContainer = cookieJar;
request.Timeout = timeout;
request.KeepAlive = true;
string jkhgj = request.Headers.ToString();
response = (HttpWebResponse)request.GetResponse();
Stream Answer = response.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
string output = _Answer.ReadToEnd().ToString();



httpwebrequest和httpwebresponse和cookie容器一样,在所有页面请求中都是持久的,唯一改变的是URL变量,它是字符串形式的地址.

从一个页面请求到下一个页面请求,用户代理和代理始终保持相同.

在此先感谢大家,这已经让我好几天了.

**更新**
通过解析响应中的cookie列表,并设法将所有cookie的路径更改为"/",我设法解决了这个问题.我知道这并不完美,但我认为这对我的情况有用.

这是什么呢?当然这完全违反了Cookie规范吗?仅当未为服务器中的cookie设置显式路径时,这才是真正的问题.我仍然会出于某种原因或更复杂的解决方案来检查此线程,因为我仍然认为此线程不正确.

解决方案

您可以尝试以下选项...

1)在您的web.config中

<sessionState cookieless= "false />

(这将禁用Cookie Munging)

2)在IE中,转到工具->互联网选项->隐私标签->高级->勾选替代自动Cookie处理->勾选始终允许会话cookie.

3)

Response.Cookies["yourcookiename"].Domain = "yourdomain.com";

这将跨指定域设置cookie.

4)

Response.Cookies["yourcookiename"].Path = "/FolderName";

这将在您的虚拟目录中为该文件夹设置cookie.对我来说,这真是太奇怪了,因为我到处都读到了关于它的读物.

1)不,我不认为这甚至在c#
2)IE设置不应对httpwebrequest产生任何影响.
3)在response.cookies [i] .domain中正确设置了域,因此遗憾地将其更改为该域将无法修复.
4)只是尝试了一下,可惜那也不起作用.

Cookies可以很好地存储和发送,但只能用于该初始页面,而不能用于同一域中的其他页面.真的很奇怪,因为这似乎不是httpwebrequest的预期行为.


它确实不是预期的功能,并且是httpwebrequest有效性的巨大空白.我在这些东西上浪费了几个月的时间,只是发现它应该在.NET 4.0中工作(不是对我来说不是),并且不会在任何早期版本的.NET中得到修复.

如果您打算创建与Web交互的应用程序,则可能需要先尝试使用另一种语言,然后再花数年时间去做一些不可能的事情.


Hey guys, I have an app that uses an httpwebrequest ( was originally a cookie enabled webclient) with a cookie container to log into a website and everything goes fine initially.

Basically I receive the cookie and check that it exists in the cookieContainer and it sends it back to the website so long as I stay on that single page. However whenever I try to navigate to any other pages on the same domain (and even in the same folder) the cookies are no longer being sent.

I usually build using .NET 2.0 but have also tried with 3.5 and found exactly the same behaviour.

Is this the expected behaviour of an httpwebrequest with a cookiecontainer attached or am I missing something in my code?

request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "text/html";
request.AllowAutoRedirect = true;
request.CookieContainer = cookieJar;
request.Timeout = timeout;
request.KeepAlive = true;
string jkhgj = request.Headers.ToString();
response = (HttpWebResponse)request.GetResponse();
Stream Answer = response.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
string output = _Answer.ReadToEnd().ToString();



The httpwebrequest and httpwebresponse are both persistent through all page requests as is the cookie container, only thing that changes is the URL variable which is an address in string form.

The user-Agent and proxy is always kept the same from one page request to the next.

Thanks in advance guys, this has been kicking my bum for days now.

**UPDATE**
I have managed to hack my way around this issue by parsing the list of cookies in response.Cookies and changing the path on all of them to "/". I know this isn''t perfect but I think it will work for my situation.

What is with this though? Surely this is completely against the spec on cookies? It is only really a problem when no explicit path is set for th cookies from the server. I will still be checking this thread for a reason or a more elequent solution to this issue as I still consider it to not be correct.

解决方案

You can try following options...

1)

<sessionState cookieless= "false />

in your web.config (this will disable Cookie Munging)

2) In IE go to tools-> internet options-> privacy tab-> advanced -> tick override automatic cookie handling -> tick always allow session cookie.

3)

Response.Cookies["yourcookiename"].Domain = "yourdomain.com";

this will set the cookie across the specified domain.

4)

Response.Cookies["yourcookiename"].Path = "/FolderName";

this will set the cookie for that folder in your virtual directory.


So the cookiecontainer doesn''t actually store cookies across a domain as standard? That seems really strange to me as everywhere I read anything on it that is how it reads.

1) No I don''t think this is even in c#
2) IE settings shouldn''t have any impact on the httpwebrequest.
3) The domain is set correctly in the response.cookies[i].domain so changing that to the domain sadly won''t fix it.
4) Just tried this and sadly that didn''t work either.

The cookies are being stored and sent fine but only for that initial page and no other pages on the same domain. Really strange as this doesn''t seem to be the intended behaviour of httpwebrequest.


It indeed is not the intended functionality and is a huge gaping hole in the effectiveness of the httpwebrequest. I have wasted months on this stuff only to find out it is supposedly working in .NET 4.0 (not for me it isn''t) and will not be fixed in any earlier version of .NET.

If you are planning on creating apps that interact with the web you may want to look into a different language before you waste years on something that is impossible.


这篇关于httpwebrequest/webclient不保留跨域的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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