简体   繁体   English

用于重定向请求的HttpWebRequest cookie

[英]HttpWebRequest cookie for redirected request

In my program I should authorize on the site, get authorization cookie value and send request to page (page1, which url like "mysite.com/lots/8188/request/4261/"), which redirect me to another (page2, which url like "mysite.com/lots/view/8188/"): 在我的程序中,我应该在网站上进行授权,获取授权Cookie的值并将请求发送至页面(第1页,其网址类似“ mysite.com/lots/8188/request/4261/”),然后将我重定向到另一个页面(第2页,网址为“ mysite.com/lots/view/8188/”):

sendingRequest.CookieContainer = new CookieContainer();
                sendingRequest.CookieContainer.Add(sendingRequest.RequestUri, new Cookie(ASPNETSessionIdCookieName, this.ASPNETSessionIdCookie));
sendingRequest.AllowAutoRedirect = true;
                sendingRequest.MaximumAutomaticRedirections = 100;
HttpWebResponse response = (HttpWebResponse)sendingRequest.GetResponse();

Ok, page1 return me expected HTTP 302 Page with expected url of page2, then sendingRequest go to page2, but it don't send ASPNETSessionIdCookieName cookie to page2 and server return me uncorrect response. 好的,第1页向我返回了预期的HTTP 302页面,并带有第2页的预期URL,然后sendRequest转到了第2页,但是它没有将ASPNETSessionIdCookieName cookie发送到第2页,服务器返回了我不正确的响应。 How can I use "ASPNETSessionIdCookieName" for redirected request? 如何将“ ASPNETSessionIdCookieName”用于重定向请求?

The auto-redireciton will follow the cookies domain/URI to check if the cookies should be sent again, once the URL is changing I believe the problem is the first parameter of the CookieContainer.Add . 一旦URL更改,auto-redireciton将遵循cookie域/ URI来检查是否应再次发送cookie,我相信问题是CookieContainer.Add的第一个参数。

You're restricting the cookie to the first page URL. 您将Cookie限制为首页URL。

Try change for something like this: 尝试更改以下内容:

sendingRequest.CookieContainer.Add(new Cookie(ASPNETSessionIdCookieName, this.ASPNETSessionIdCookie));

If you want to restrict the cookie only to that domain you can them use Cookie four parameters constructor. 如果您只想将Cookie限制为该域,则可以使用Cookie四个参数构造函数。

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

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