Monday, August 29, 2016

How to download HTML page through C# console application?

Hello friends, in this article I will show that how to retrieve web page or download web page source to do so one should have basic knowledge of visual studio and c# programming language.

Here is the one line that will help to retrieve webpage with C#

Use System.Net.WebClient class to use below code.


System.Console.WriteLine(new System.Net.WebClient().DownloadString(url));


How to download web page with the help c# console application.


using System.Net;
//...
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{

    client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");

    // Or you can get the file content without saving it:

    string htmlCode = client.DownloadString("http://yoursite.com/page.html");
    //...

}



There are two ways directly save it to localfile.html or you can get the code and get it into the string variable.

No comments: