无代理下载速度慢

发布于 2024-12-24 22:26:29 字数 1455 浏览 0 评论 0原文

我编写了一些调用方法 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 = nullwc.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2024-12-31 22:26:29
IWebProxy proxy = GlobalProxySelection.GetEmptyWebProxy();
wc.Proxy = proxy;
IWebProxy proxy = GlobalProxySelection.GetEmptyWebProxy();
wc.Proxy = proxy;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文