WebClient 非常慢

发布于 2024-11-29 01:58:17 字数 1156 浏览 0 评论 0原文

我的网络客户端有问题。

它非常慢。从一个网站下载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 技术交流群。

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

发布评论

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

评论(3

无可置疑 2024-12-06 01:58:17

这里可能存在两个问题(我之前在自己的程序中也注意到过):

  • 第一个请求花费了异常长的时间:发生这种情况是因为WebRequest默认情况下会在第一次启动时检测并加载代理设置,这可能需要相当长的时间。要阻止这种情况,只需将代理属性 (WebRequest.Proxy) 设置为 null,它将绕过检查(前提是您可以直接访问互联网)
  • 您一次无法下载超过 2 个项目:默认情况下,您只能同时打开 2 个 HTTP 连接。要更改此设置,请将 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):

  • The first request takes an abnormally long time: This occurs because 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) to null and it'll bypass the check (provided you can directly access the internet)
  • You can't download more than 2 items at once: By default, you can only have 2 simultaneous HTTP connections open. To change this, set ServicePointManager.DefaultConnectionLimit to something larger. I usually set this to int.MaxValue (just make sure you don't spam the host with 1,000,000 connections).
北方的韩爷 2024-12-06 01:58:17

如果与检查的初始代理设置相关,有几个选项:

  1. 禁用 Internet Explorer 中的自动代理检测设置
  2. 将代理设置为空:

    WebClient.Proxy = null

  3. 应用程序启动时将默认 Web 代理设置为 null:

    WebRequest.DefaultWebProxy = null;

在较旧的 .NET 代码中,您通常会这样写(但现在首选 null),而不是设置为 null:

webclient.Proxy = GlobalProxySelection.GetEmptyWebProxy();

There are a few options if it is related to the initial proxy settings being checked:

  1. Disable the automatic proxy detection settings in Internet Explorer
  2. Set the proxy to null:

    WebClient.Proxy = null

  3. 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):

webclient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
撑一把青伞 2024-12-06 01:58:17

也许它会对某人有所帮助。某些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文