【发布时间】:2011-10-22 18:30:11
【问题描述】:
我的 Webclient 有问题。
速度很慢。从一个网站下载字符串大约需要 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 正在发送和接收数千个数据包。
【问题讨论】:
-
WebClient 不是特别慢,问题肯定出在其他地方……也许你下载的网站很慢?
-
您应该尝试使用像 WireShark 这样的流量记录器来确定延迟是否确实在单个请求和响应帧之间(由网络/服务器减速引起)或者是因为客户端代码行为不端并发送出去请求过多或某些此类逻辑错误。
-
还在循环中执行多个下载以“预热”该过程(例如消除程序集的加载)。
-
更新:在wireshark中,我看到单个DownladString正在发送和接收数千个数据包。请帮帮我。
-
如果您要下载的内容真的很大,这是您应该期待的。如果手动下载网址,有多大?
标签: c# .net networking httpwebrequest webclient