【问题标题】:How can I get the cookies set by "Set-Cookie" in a HttpWebRequest?如何在 HttpWebRequest 中获取“Set-Cookie”设置的 cookie?
【发布时间】:2013-03-08 18:52:28
【问题描述】:

我希望下面的代码会产生一个非零值:

var webRequest = (HttpWebRequest) WebRequest.Create(loginUrl);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
Console.WriteLine(webResponse.Cookies.Count);

webRespone.Cookies 中似乎没有显示任何 cookie。我很肯定 cookie 就在那里,因为我正在用 Fiddler 嗅探数据。这是我得到的回复:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: __Abc=Def; path=/; HttpOnly
PS-ResponseTime: 00:00:00.0624001
PS-Build: 2013-03-19-11-36-59
PS-Node: 02
Date: Tue, 19 Mar 2013 21:14:51 GMT
Content-Length: 57872

这与cookie是HttpOnly这一事实有什么关系吗?

编辑

看来我可以让他们通过HttpWebRequestCookieContainer,如果我打算继续进行一系列请求/响应,这肯定很有用。但是为什么我不能通过HttpWebResponse.Cookies 字段访问它们呢?

谢谢

【问题讨论】:

  • 你是如何得到 Set-Cookie 的:__Abc=Def;路径=/; HttpOnly 在 http 响应中?

标签: c# httpwebrequest httprequest httpresponse httpwebresponse


【解决方案1】:

您是对的,您需要将请求传递给 CookieContainer 的实例,然后引用该实例以查看 cookie。基本上,HttpWebResponse 不会直接在响应中公开标记为 HttpOnly 的 cookie。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2020-08-16
    • 2015-09-12
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2011-07-21
    相关资源
    最近更新 更多