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
Dependency Injection - New Instance Required in Several of a Classes Methods
Log4Net, How to Add a Custom Field to My Logging
Cssrewriteurltransform with or Without Virtual Directory
Return Content with Ihttpactionresult for Non-Ok Response
How to Convert a Class into Dictionary<String,String>
Where to Find "Microsoft.Visualstudio.Testtools.Unittesting" Missing Dll
Sortedlist<>, Sorteddictionary<> and Dictionary<>
Datetime.Parse("2012-09-30T23:00:00.0000000Z") Always Converts to Datetimekind.Local
Calling a Method in Parent Page from User Control
Find Methods That Have Custom Attribute Using Reflection