【问题标题】:HttpWebRequest cookie for redirected request用于重定向请求的 HttpWebRequest cookie
【发布时间】:2014-10-08 20:52:45
【问题描述】:

在我的程序中,我应该在网站上进行授权,获取授权 cookie 值并将请求发送到页面(page1,其 url 类似于“mysite.com/lots/8188/request/4261/”),将我重定向到另一个( page2,其中 url 像“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();

好的,page1 向我返回预期的 HTTP 302 页面,预期的 url 为 page2,然后将发送请求转到 page2,但它不向 page2 发送 ASPNETSessionIdCookieName cookie,服务器返回不正确的响应。 如何使用“ASPNETSessionIdCookieName”进行重定向请求?

【问题讨论】:

    标签: c# httpwebrequest


    【解决方案1】:

    自动重定向将跟随 cookie 域/URI 来检查是否应该再次发送 cookie,一旦 URL 发生变化,我相信问题是 CookieContainer.Add 的第一个参数。

    您将 cookie 限制为首页 URL。

    尝试更改如下:

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

    如果您想将 cookie 仅限制在该域中,您可以使用 Cookie 四参数构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多