I provide an HTTP web service and one of my users is using C# WebClient class on a Windows 2003 machine to retrieve data from my website. My user says that WebClient is creating many browser instances and needs to be closed. How can he close the browser after it's created?
His code:
Byte[] requestedHTML;
WebClient client = new WebClient();
requestedHTML = client.DownloadData("http://abcabc.com/abc");
UTF8Encoding objUTF8 = new UTF8Encoding();
string returnMessage = objUTF8.GetString(requestedHTML);
p.s. Apologies if this sounds amateur, I'm very new to C#.
WebClient
does not use a browser - it it just a wrapper around the underlying protocol. You should add a using
, but this has nothing to do with "many browser instances":
using(WebClient client = new WebClient())
{
return client.DownloadString("http://abcabc.com/abc");
}
The WebClient class in the .NET Framework holds onto some system resources which are required to access the network stack in Microsoft Windows. The behavior of the CLR will ensure these resources are eventually cleaned up.
However, if you manually call Dispose
or use the using-statement
, you can make these resources be cleaned up at more predictable times. This can improve the performance of larger programs.
using(WebClient client = new WebClient())
{
// Do your operations here...
}
You can refer this beautiful tutorial: http://www.dotnetperls.com/webclient
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With