NotDan
NotDan

Reputation: 32233

How can I download HTML source in C#

How can I get the HTML source for a given web address in C#?

Upvotes: 124

Views: 204013

Answers (5)

Hakan Fıstık
Hakan Fıstık

Reputation: 19521

The newest answer
HttpClient is considered the new API and it should replace the old ones
(WebClient and WebRequest)

HttpClient client = new HttpClient();   // actually only one object should be created by Application
string page = await client.GetStringAsync("page URL here");

Upvotes: 37

Christian C. Salvadó
Christian C. Salvadó

Reputation: 828090

You can download files with the WebClient class:

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

Upvotes: 199

Xenon
Xenon

Reputation: 815

You can get the HTML source with:

var html = new System.Net.WebClient().DownloadString(siteUrl)

Upvotes: 17

Diego Jancic
Diego Jancic

Reputation: 7460

Basically:

using System.Net;
using System.Net.Http;  // in LINQPad, also add a reference to System.Net.Http.dll

WebRequest req = HttpWebRequest.Create("http://google.com");
req.Method = "GET";

string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
    source = reader.ReadToEnd();
}

Console.WriteLine(source);

Upvotes: 42

Xilmiki
Xilmiki

Reputation: 1502

@cms way is the more recent, suggested in MS website, but I had a hard problem to solve, with both method posted here, now I post the solution for all!

problem: if you use an url like this: www.somesite.it/?p=1500 in some case you get an internal server error (500), although in web browser this www.somesite.it/?p=1500 perfectly work.

solution: you have to move out parameters, working code is:

using System.Net;
//...
using (WebClient client = new WebClient ()) 
{
    client.QueryString.Add("p", "1500"); //add parameters
    string htmlCode = client.DownloadString("www.somesite.it");
    //...
}

here official documentation

Upvotes: 11

Related Questions