C# WebClient下载字符串(几个字符的页面)太慢了

C# WebClient下载字符串(几个字符的页面)太慢了,c#,performance,webclient,downloadstring,C#,Performance,Webclient,Downloadstring,我正在尝试从URL下载字符串。不幸的是,它非常慢 这是我的密码: // One of these types for two bad solutions anyway // byte[] result = new byte[12]; // string result; using (var webClient = new System.Net.WebClient()) { String url = "http://bg2.cba.pl/re

我正在尝试从URL下载字符串。不幸的是,它非常慢

这是我的密码:

    // One of these types for two bad solutions anyway
    // byte[] result = new byte[12];
    // string result;
    using (var webClient = new System.Net.WebClient())
    {
        String url = "http://bg2.cba.pl/realmIP.txt";
        //result = webClient.DownloadString(url); // slow as hell
        //webClient.OpenRead(url).Read(result, 0, 12); // even slower
    }
大约需要4-5秒,这对我来说似乎很不合适

此url的内容是IP

 XX.YYY.ZZ.FF

我尝试了你的代码并添加了一些输出

        using (var webClient = new System.Net.WebClient())
        {
            Stopwatch timer = Stopwatch.StartNew();
            String url = "http://bg2.cba.pl/realmIP.txt";
            timer.Stop();
            TimeSpan timespan = timer.Elapsed;
            String tex1 = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);


            timer = Stopwatch.StartNew();
            String result = webClient.DownloadString(url); // slow as hell
            timespan = timer.Elapsed;
            String tex2 = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);


            timer = Stopwatch.StartNew();
            Stream stream = webClient.OpenRead(url);
            timespan = timer.Elapsed;
            String tex3 = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);

            timer = Stopwatch.StartNew();
            byte[] result2 = new byte[12];
            int val = webClient.OpenRead(url).Read(result2, 0, 12); // even slower
            timespan = timer.Elapsed;
            String tex4 = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);

            textBox1.Text = result;
            t1.Text = tex1;
            t2.Text = tex2;
            t3.Text = tex3;
            t4.Text = tex4;
        }
结果如下

你的代码似乎没问题。
检查您的防火墙和所有涉及的内容

已修复,我想很抱歉将此问题放在这里,但是。。。这是工作代码

string result;
using (var webClient = new System.Net.WebClient())
{
    webClient.Proxy=null;
    String url = "http://bg2.cba.pl/realmIP.txt";
    result = webClient.DownloadString(url);
}

只需将Proxy设置为null

这显然是您线路/电脑/防火墙的问题

您可以在线测试:

大约需要500毫秒

在您自己的答案后更新

如果您不想使用代理,则应按照以下说明使用:


你有没有使用Wireshark或类似的工具来观察时间的走向?我没有使用WS,因为我不知道如何使用它it@BartłomiejSobieszek读入Wireshark然后我刚刚测试了你的代码,它似乎工作得很快。你有防火墙或中间的东西吗?好的,我确定了,我必须将代理设置为NULL):使用<代码> GualAdLogySoop.GeFunTyWebPro()/<代码>而不是NULL。请阅读我的答案以了解更多详细信息。我必须使用“client.Proxy=null;”来加快代码的运行速度。(C#)webClient.Proxy=null;这对meNote有效:尽管
GlobalProxySelection.GetEmptyWebProxy()
已被弃用,但这是我能够加速WebClient请求的唯一方法。如果有人知道实现相同行为的任何非弃用方法,请告诉我。他们建议现在将.Proxy设置为“null”
webClient.Proxy=GlobalProxySelection.GetEmptyWebProxy();