System.Net.Webclient Unreasonably Slow

C# WebClient acting slow the first time

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.

WebClient.DownloadDataAsync is freezing my UI

Now that we've got full code, I can say I'm definitely not seeing the problem - not quite as described, anyway.

I've got a bit of logging to indicate just before and after the DownloadDataAsync calls, and when the completed handler is fired. If I download a large file over 3G, there is a pause between "before" and "after" but the UI comes up ages before the file completes downloading.

I have a suspicion that the connect is done synchronously, but the actual download is asynchronous. That's still unfortunate, of course - and possibly punting all of that into a different thread is the way to go - but if I'm right it's at least worth knowing about.

Trouble with downloading data from web site

Some websites look at the UserAgent string in HTTP headers to see if the caller is an actual web browser. If you use the HttpWebRequest class you have more control over the call and can spoof the UserAgent string to simulate a real web browser. Try something like the following to make it work:

HttpWebRequest request = WebRequest.Create("http://limun.hr/main.aspx?id=18") as HttpWebRequest;
request.UserAgent = "Mozilla /5.0 (Compatible MSIE 9.0;Windows NT 6.1;WOW64; Trident/5.0)";
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(););
string content = reader.ReadToEnd();
reader.Close();
response.Close();
Console.WriteLine(content);


Related Topics



Leave a reply



Submit