Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WebClient acting slow the first time

I am using a WebClient to download a string from a website (which just contains plain text, nothing else), so I use the DownloadString method:

WebClient wc = new WebClient();
string str = wc.DownloadString("http://blah");

It works fine, but the problem is that the first time it downloads the string it takes a long time, like 5 seconds. After that it works fast. Why does this happen and how can overcome this problem?

like image 208
rayanisran Avatar asked Feb 08 '11 11:02

rayanisran


2 Answers

Setting the Proxy property of your WebClient object to null should eliminate the delays you're seeing. Alternatively if you've configured your system to use a proxy it can be retrieved with WebRequest.GetSystemWebProxy. The second method should eliminate the delay in either case.

like image 116
T3hc13h Avatar answered Sep 25 '22 14:09

T3hc13h


I noticed the same thing. DotTrace shows it's spending the majority of its time enumerating proxy options:

like image 27
Drew Noakes Avatar answered Sep 22 '22 14:09

Drew Noakes