gpt4 book ai didi

c# - WebClient CookieContainer 在 .NET 4.0+ 中运行良好,但在早期版本中运行不佳

转载 作者:太空狗 更新时间:2023-10-29 23:01:12 26 4
gpt4 key购买 nike

我正在开发一个使用 WebClient 的应用程序。我有一个扩展基本 WebClient 功能的类:

public class WebClientEx : WebClient
{
private CookieContainer _cookieContainer = new CookieContainer();

protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _cookieContainer;
(request as HttpWebRequest).AllowAutoRedirect = true;
(request as HttpWebRequest).Timeout = 10000;
}
return request;
}
}

我使用 WebClientEx 登录站点并请求一些信息。它适用于 4.0 和 4.5,但在 3.5、3.0 等早期版本中不起作用。我添加了一些调试代码,在早期版本中它说 cookie 容器中有 0 个 cookie,而 4.0+ 说有应该是两个 cookie。

所以原因可能是 .NET Framework 的早期版本在 cookie 容器中存储 cookie 时存在一些问题。如何解决?

最佳答案

我已确认 .NET 3.5 与 .NET 4.0 中的行为不同。是用下面的代码来测试的:

Uri sourceUri = new Uri(@"http://www.html-kit.com/tools/cookietester/");
WebClientEx webClientEx = new WebClientEx();
webClientEx.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
webClientEx.UploadString(sourceUri, "cn=MyCookieName&cv=MyCookieValue");
var text = webClientEx.DownloadString(sourceUri);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new MemoryStream(Encoding.ASCII.GetBytes((text))));
var node = doc.DocumentNode.SelectNodes("//div").Single(n => n.InnerText.StartsWith("\r\nNumber of cookies received:"));
Debug.Assert(int.Parse(node.InnerText.Split(' ')[4]) == 1);

当然,这并没有回答您的问题;但除了说它可能已在 .NET 4.0 中修复并且尚未将修复程序放入 .NET 3.5 或更早版本之外,我看不出为什么存在行为差异。

我用 HttpWebRequest 尝试了类似的事情并遇到了同样的问题(在 4 中有效,但不是之前的):

HttpWebRequest webreq = ((HttpWebRequest) (WebRequest.Create(sourceUri)));
CookieContainer cookies = new CookieContainer();

var postdata = Encoding.ASCII.GetBytes("cn=MyCookieName&cv=MyCookieValue");

webreq.CookieContainer = cookies;
webreq.Method = "POST";
webreq.ContentLength = postdata.Length;
webreq.ContentType = "application/x-www-form-urlencoded";

Stream webstream = webreq.GetRequestStream();
webstream.Write(postdata, 0, postdata.Length);
webstream.Close();

using (WebResponse response = webreq.GetResponse())
{
webstream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(webstream))
{
String responseFromServer = reader.ReadToEnd();
var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new MemoryStream(Encoding.ASCII.GetBytes((responseFromServer))));
var node =
doc.DocumentNode.SelectNodes("//div").Single(n => n.InnerText.StartsWith("\r\nNumber of cookies received:"));
Debug.Assert(int.Parse(node.InnerText.Split(' ')[4]) == 1);
}
}

因此,HttpWebRequest(WebClient 使用)似乎有问题。这可能是新的,因为我看到人们在 4.0 发布之前使用过这样的代码(可能在 3.50 之前,他们说它有效。

如果情况紧急,我建议联系 Microsoft 支持部门。如果您拥有 MSDN 许可证,则以下链接将详细说明如何使用随附的 MSDN 支持票证提出支持请求:http://msdn.microsoft.com/en-us/subscriptions/bb266240.aspx如果您没有 MSDN,您可以联系支持人员,详情如下:https://support.microsoft.com/oas/default.aspx?Gprid=8291&st=1&wfxredirect=1&sd=gn

如果不那么紧急,那么您可以在 http://connect.microsoft.com/VisualStudio 记录问题看看您是否收到了解决方法的回复。

关于c# - WebClient CookieContainer 在 .NET 4.0+ 中运行良好,但在早期版本中运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12029769/

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