无代理下载速度慢
我编写了一些调用方法 webClient.DownloadString 的代码。当我运行程序分析器时,显示此方法占用了程序中 98% 的时间(不是处理器时间),并且下载速度太慢(大约是最大下载速度的 10%)。
当我使用 Fiddler 运行程序时,下载速度增加到最大下载速度的 75% - 90%。
我的错误在哪里?如何提高应用程序的下载速度?
这是我的代码。
WebClient wc = new WebClient();
wc.Encoding = Encoding.GetEncoding("1251");
wc.Proxy = null;
HtmlDocument html = new HtmlDocument();
html.LoadHtml(wc.DownloadString("http://some-site.com/" + htmlUrl));
var c1 = html.GetElementbyId("content");
foreach (var c2 in c1.ChildNodes.Where(c => c.Name == "div"))
{
foreach (var c3 in c2.ChildNodes.Where(c => c.Name == "a"))
{
var leftIndex = c3.InnerText.IndexOf('(');
var rightIndex = c3.InnerText.LastIndexOf(')');
var id = c3.InnerText.Substring(0, leftIndex - 1).Replace(" ", "");
var name = c3.InnerText.Substring(leftIndex + 1, rightIndex - leftIndex - 1);
SaveInDB(int.Parse(id), name);
}
}
我已经在多个线程中运行它以提高下载速度。
我尝试使用其他变体 wc.Proxy = null
或 wc.Proxy = GlobalProxySelection.GetEmptyWebProxy()
或 WebProxy.GetDefaultProxy()
或wc.Proxy = new WebProxy()
。结果我的下载速度总是很低。
关闭“控制面板”/“Internet 选项”-> 上的“自动检测设置”连接选项卡 -> LAN 设置也没有提高下载速度。
I have written some code that invoke method webClient.DownloadString. When I ran my programe profiler show that this method takes 98% of time in the program (not processor time), and download speed is too slow (about 10% of maximal download speed).
When I run my program with Fiddler download speed increases to 75% - 90% of maximal download speed.
Where is my error? And How can I increase download speed of my application?
Here is my code.
WebClient wc = new WebClient();
wc.Encoding = Encoding.GetEncoding("1251");
wc.Proxy = null;
HtmlDocument html = new HtmlDocument();
html.LoadHtml(wc.DownloadString("http://some-site.com/" + htmlUrl));
var c1 = html.GetElementbyId("content");
foreach (var c2 in c1.ChildNodes.Where(c => c.Name == "div"))
{
foreach (var c3 in c2.ChildNodes.Where(c => c.Name == "a"))
{
var leftIndex = c3.InnerText.IndexOf('(');
var rightIndex = c3.InnerText.LastIndexOf(')');
var id = c3.InnerText.Substring(0, leftIndex - 1).Replace(" ", "");
var name = c3.InnerText.Substring(leftIndex + 1, rightIndex - leftIndex - 1);
SaveInDB(int.Parse(id), name);
}
}
I have run it in several threads to increase download speed.
And I tried to use other variants wc.Proxy = null
or wc.Proxy = GlobalProxySelection.GetEmptyWebProxy()
or WebProxy.GetDefaultProxy()
or wc.Proxy = new WebProxy()
. I always had a low download speed as result.
Turning off "Automatically detect settings" on the "Control Panel"/"Internet Options" -> Connection tab -> LAN settings also didn't increase download speed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)