我对Webclient有意见。
它非常慢。从一个网站到downloadString大约需要3-5秒.我没有任何网络问题。
这是我修改过的WebClient。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace StatusChecker
{
class WebClientEx: WebClient
{
public CookieContainer CookieContainer { get; private set; }
public WebClientEx()
{
CookieContainer = new CookieContainer();
ServicePointManager.Expect100Continue = false;
Encoding = System.Text.Encoding.UTF8;
WebRequest.DefaultWebProxy = null;
Proxy = null;
}
public void ClearCookies()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return request;
}
}
}更新:在wireshark中,我看到单个DownladString正在发送和接收数千个数据包。
发布于 2011-08-09 00:52:50
这里可能有两个问题(我以前在自己的程序中也注意到了):
WebRequest在第一次启动时默认检测并加载代理设置,这可能需要相当长的时间。要阻止这种情况,只需将代理属性(WebRequest.Proxy)设置为null,它将绕过检查(前提是您可以直接访问WebRequest.Proxy,不能同时下载多个项目:默认情况下,只能同时打开两个HTTP )。要改变这一点,请将ServicePointManager.DefaultConnectionLimit设置为更大的东西。我通常将其设置为int.MaxValue (只需确保不使用100万int.MaxValue垃圾邮件)发布于 2017-10-17 06:43:35
如果它与正在检查的初始代理设置相关,则有几个选项:
在应用程序启动时,WebClient.Proxy =null
WebRequest.DefaultWebProxy = null;
在较早的.NET代码中,不是将其设置为null,而是编写此代码(但现在首选为null ):
webclient.Proxy = GlobalProxySelection.GetEmptyWebProxy();发布于 2015-10-16 10:52:43
也许它会对某人有帮助。一些web服务支持压缩(gzip或其他)。因此,您可以为请求添加接受编码头,然后为web客户端实例启用automatic decompression。Chrome就是这样工作的。
https://stackoverflow.com/questions/6988981
复制相似问题