Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close browser after creating C# WebClient class?

Tags:

c#

webclient

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#.

like image 901
Joshua Avatar asked May 28 '13 07:05

Joshua


2 Answers

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");
}
like image 185
Marc Gravell Avatar answered Oct 27 '22 01:10

Marc Gravell


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

like image 26
Santosh Panda Avatar answered Oct 27 '22 01:10

Santosh Panda