Joshua
Joshua

Reputation: 1737

How to close browser after creating C# WebClient class?

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

Upvotes: 6

Views: 6020

Answers (2)

Santosh Panda
Santosh Panda

Reputation: 7341

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062755

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");
}

Upvotes: 8

Related Questions