WebClient 非常慢
我的网络客户端有问题。
它非常慢。从一个网站下载String大约需要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正在发送和接收数千个数据包。
I have problem with Webclient.
It is very slow. It takes about 3-5 seconds to downloadString from one website.
I don't have any network problems.
This is my Modifed 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;
}
}
}
UPDATE:
In wireshark I saw that single DownladString is sending and receiving few thousands packets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里可能存在两个问题(我之前在自己的程序中也注意到过):
WebRequest
默认情况下会在第一次启动时检测并加载代理设置,这可能需要相当长的时间。要阻止这种情况,只需将代理属性 (WebRequest.Proxy
) 设置为null
,它将绕过检查(前提是您可以直接访问互联网)ServicePointManager.DefaultConnectionLimit
设置为更大的值。我通常将其设置为int.MaxValue
(只要确保您不会向主机发送垃圾邮件,连接数为 1,000,000)。There may be two issues at hand here (that I've also noticed in my own programs previously):
WebRequest
by default detects and loads proxy settings the first time it starts, which can take quite a while. To stop this, simply set the proxy property (WebRequest.Proxy
) tonull
and it'll bypass the check (provided you can directly access the internet)ServicePointManager.DefaultConnectionLimit
to something larger. I usually set this toint.MaxValue
(just make sure you don't spam the host with 1,000,000 connections).如果与检查的初始代理设置相关,有几个选项:
将代理设置为空:
WebClient.Proxy = null
应用程序启动时将默认 Web 代理设置为 null:
WebRequest.DefaultWebProxy = null;
在较旧的 .NET 代码中,您通常会这样写(但现在首选 null),而不是设置为 null:
There are a few options if it is related to the initial proxy settings being checked:
Set the proxy to null:
WebClient.Proxy = null
On application startup set the default webproxy to null:
WebRequest.DefaultWebProxy = null;
In older .NET code instead of setting to null, you used to write this (but null is now preferred):
也许它会对某人有所帮助。某些 Web 服务支持压缩(gzip 或其他)。因此,您可以为请求添加 Accept-Encoding 标头,然后启用自动解压缩 用于 Web 客户端实例。 Chrome 就是这样工作的。
Maybe it will help somebody. Some web services support compression (gzip or other). So you can add Accept-Encoding header for your requests and then enable automatic decompression for web client instance. Chrome works in that way.