4

I am trying to download a string from an URL. Unfortunately, it is very slow.

Here is my code:

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

It takes about 4-5 seconds, which seems very inappropriate to me...

Content of this url is IP

 XX.YYY.ZZ.FF
glglgl
  • 89,107
  • 13
  • 149
  • 217
Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40

3 Answers3

11

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

Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
4

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

You can test it online:

http://goo.gl/XRqLjn

it takes about 500 milliseconds

enter image description here

UPDATE after your own answer

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

webClient.Proxy=GlobalProxySelection.GetEmptyWebProxy();
giammin
  • 18,620
  • 8
  • 71
  • 89
  • Note: Even though `GlobalProxySelection.GetEmptyWebProxy()` has become deprecated, this is the only way I was able to speed up the WebClient requests. If anyone knows any non-deprecated way of achieving the same behavior, please let me know. – Elad Nava Oct 01 '15 at 20:23
  • 1
    they recommend to set .Proxy to "null" now – hanzolo Aug 15 '18 at 19:38
2

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

enter image description here

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

Bongo
  • 2,933
  • 5
  • 36
  • 67