Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

A tip when making web Requests

Sorry this doesn't have actual screeshots of it, my pc has some bug and is being weird.

I wrote this a while back under another username, thought it mignt be helpful on here for all the noobies out there.







Often times when I use programs that require WebRequests, they block the calling thread. Simple terms, the user interface freezes until it's done.


There are a few remedies to fix this but you might still run into UI freezing.

Create a new thread:

Code:
new Thread(new ThreadStart(() => {
using (WebClient wc = new WebClient())
{
string webString = wc.DownloadString("http://www.google.com");
Console.WriteLine(webString);
}

})).Start();





Or WebClient offers a method to Download Asynchronously - https://msdn.microsoft.com/en-us/library...10%29.aspx

Code:
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted += (sender, e) =>
{
Console.WriteLine(e.Result);
};

wc.DownloadStringAsync(new Uri("http://google.com"));
}





Now, there maybe a time where you go to put the web request in a new thread or use DownloadStringAsync() and it still blocks the UI. Why?


The simple fix is adding this line to your code:

Code:
WebRequest.DefaultProxy = null;


Why does this work?

Because before the request is even sent, It will check Internet Explorers proxy setting by default which may cause in a delay/ui freeze even though you took precautions. So setting it the null ahead of time skips that process and solves problems.
wrong section
you should post this in "C# Programming Language"
Use code tags next time, please.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by evilgnomeJan 11, 2022 05:39 AM 2 195 Jan 11, 2022 05:39 AM
Last Post: Charger
[Python] by 05317832892Dec 28, 2018 07:33 AM 2 113 Dec 28, 2018 07:33 AM
Last Post: 05317832892
[Python] by mzabossSep 23, 2016 06:57 AM 10 432 Sep 23, 2016 06:57 AM
Last Post: SIM-1866423