WebClient download string (a few chars page) is so slow

老子叫甜甜 提交于 2019-12-05 12:00:19

Fixed, sorry for putting this question here I guess, but... here is working code

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

Just set Proxy to null

It is clearly a problem with you line/pc/firewall

You can test it online:

http://goo.gl/XRqLjn

it takes about 500 milliseconds

UPDATE after your own answer

If you want to use no proxy you should use GetEmptyWebProxy() as stated on msdn:

webClient.Proxy=GlobalProxySelection.GetEmptyWebProxy();

I tried your code and added some output to it.

        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;
        }

with the following Result

Your code seems to be okay. Check your Firewall and all the stuff that is involved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!