gpt4 book ai didi

c# - .NET CookieContainer 帮助

转载 作者:行者123 更新时间:2023-11-30 19:00:43 25 4
gpt4 key购买 nike

我最近遇到了 CookieContainer 的一些问题。要么是我做错了什么,要么是 CookieContainer 对象存在某种错误。它似乎没有使用某些 Set-Cookie header 更新 cookie 集合。

这可能是一篇冗长的文章,我深表歉意,但我希望尽可能完整,所以我将列出我的 HTTP 嗅探日志以及我的实际实现代码。

    public bool SendRequest(HttpWebRequest request, IDictionary<string, string> data, int retries)
{
// copy request in case request instance already failed
HttpWebRequest newRequest = (HttpWebRequest)HttpWebRequest.Create(request.RequestUri);
newRequest.Method = request.Method;

// if POST data was provided, write it to the stream
if (data != null && data.Count != 0)
{
StreamWriter writer = new StreamWriter(newRequest.GetRequestStream());
writer.Write(createPostString(data));
writer.Close();
}

// set request with global cookie container
newRequest.CookieContainer = this.cookieJar;

try
{
using (HttpWebResponse resp = (HttpWebResponse)newRequest.GetResponse())
{
//CookieCollection newCooks = getCookies(resp.Headers);
//updateCookies(newCooks);
this.cookieJar = newRequest.CookieContainer;
this.Html = getResponseString(resp);

/* remainder snipped */


所以有代码,这是我在 Fiddler 中嗅探到的两个响应请求:


请求 1

POST /login/ HTTP/1.1
Host: www.site.com
Content-Length: 47
Expect: 100-continue
Connection: Keep-Alive

响应 1

HTTP/1.1 200 OK
Date: Wed, 02 Dec 2009 17:03:35 GMT
Server: Apache
Set-Cookie: tcc=one; path=/
Set-Cookie: cust_id=2702585226; domain=.site.com; path=/; expires=Mon, 01-Jan-2011 00:00:00 GMT
Set-Cookie: cust_session=12%2F2%2F2009%20%2012%3A3%3A35; domain=.site.com; path=/; expires=Wed 2-Dec-2009 17:33:35
Set-Cookie: refer_id_persistent=0000; domain=.site.com; path=/; expires=Fri 2-Dec-2011 17:3:35
Set-Cookie: refer_id=0000; domain=.site.com; path=/
Set-Cookie: private_browsing_mode=off; domain=.site.com; path=/; expires=Fri, 01-Jan-2010 17:03:35 GMT
Set-Cookie: member_session=UmFuZG9tSVYL%5BS%5D%5BP%5DfhH77bYaVoS9j9Yd8ySRkyHHz%5BS%5Dk0S8MVsQ6AyraNlcdcCRC0RkB%5BP%5DfBYVM4vn6JQ3HlJxT3GlJi1RZiMGQaITg7HN9dpu9oRbZgMjhJlXXa%5BP%5D7pFSjqDIZWRr3LAfnhh3btv4E3rvVH42CeOP%5BS%5Dx6kDyvrokQEHyIHPGi7zswZbuHrUdx2XKEKKJzw1unDWfw0LZWjoehAs0QgSOz6Nzp8P4Hp8hqrULdIMch6acPT%5BS%5DbKV8zwugBIcjr5dI3rVR%5BP%5Dv42rsTtQB7dyb%5BP%5DRKb8Y83cGqhHM33hP%5BP%5DUtmbDC1PPfr%5BS%5DPC23lAO%5BS%5DmQ3mOy9x4pgQSOfp40XSfzgVg3EavITaxHBeI5nO3%5BP%5D%5BS%5D2rSDthDfuEm4sT9i6UF3sYd1vlOL0IC9ZsVatV1yhhpQ%5BE%5D%5BE%5D; domain=.site.com; path=/; expires=Fri, 01-Jan-2010 17:03:35 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

请求2

GET /test?search=jjkjf HTTP/1.1
Host: www.site.com
Cookie: tcc=one; cust_id=2702585226; private_browsing_mode=off; member_session=UmFuZG9tSVYL%5BS%5D%5BP%5DfhH77bYaVoS9j9Yd8ySRkyHHz%5BS%5Dk0S8MVsQ6AyraNlcdcCRC0RkB%5BP%5DfBYVM4vn6JQ3HlJxT3GlJi1RZiMGQaITg7HN9dpu9oRbZgMjhJlXXa%5BP%5D7pFSjqDIZWRr3LAfnhh3btv4E3rvVH42CeOP%5BS%5Dx6kDyvrokQEHyIHPGi7zswZbuHrUdx2XKEKKJzw1unDWfw0LZWjoehAs0QgSOz6Nzp8P4Hp8hqrULdIMch6acPT%5BS%5DbKV8zwugBIcjr5dI3rVR%5BP%5Dv42rsTtQB7dyb%5BP%5DRKb8Y83cGqhHM33hP%5BP%5DUtmbDC1PPfr%5BS%5DPC23lAO%5BS%5DmQ3mOy9x4pgQSOfp40XSfzgVg3EavITaxHBeI5nO3%5BP%5D%5BS%5D2rSDthDfuEm4sT9i6UF3sYd1vlOL0IC9ZsVatV1yhhpQ%5BE%5D%5BE%5D


正如您所见,用于每个请求的 CookieContainer (this.cookieJar) 没有为 refer_id、cust_session、refer_id_persistent 获取 Set-Cookie header 。然而,它确实会获取 cust_id、private_browsing_mode、tcc 和 member_session...知道为什么会这样吗?

最佳答案

只是想更新这篇文章以防其他人遇到这个问题。问题是 .NET 符合 cookie 标签的 RFC 规范,但并非所有站点都这样做。所以,最终,问题不在于微软,也不在于 .NET。 (尽管 IE 可以很好地管理 cookie,因此最好使用相同的解析方法重写其 .NET cookie 解析方法)问题是不遵循 RFC specifications 的网站.

尽管如此,我经常遇到的一个问题是网站会在其 cookie 的到期日期中使用逗号。 .NET 将这些解释为不同 cookie 字段之间的分隔符,并从 cookie 中删除结尾和之后的所有内容。

RFC spec : "Cookie:, 后跟一个或多个 cookie 的逗号分隔列表。"根据 RFC 文档,此问题的一个简单解决方案是 Web 服务器将值用逗号括在引号中。然而,没有 RFC 警察,所以我们只能希望人们遵守规则。

关于c# - .NET CookieContainer 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1834522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com