121

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

John Smith
  • 7,243
  • 6
  • 49
  • 61
NotDan
  • 31,709
  • 36
  • 116
  • 156

5 Answers5

198

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");
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Should note: if more control is needed, look at the HttpWebRequest class (e.g. being able to specify authentication). – Richard Mar 01 '09 at 15:12
  • 1
    Yes, HttpWebRequest gives you more control, although you can do POST requests with WebClient, using client.UploadData(uriString,"POST",postParamsByteArray); – Christian C. Salvadó Mar 01 '09 at 17:51
  • 1
    Wouldn't it be prudent to catch WebException's around this? Maybe that was assumed. Any other exceptions or errors need to be caught with this method? – John Washam Feb 21 '14 at 21:50
  • 4
    @JohnWasham - yes, it would be prudent to catch exceptions here. Thankfully however, most StackOverflow respondents keep example code as clear and concise as possible. Making example code closer to "real life" would just add noise. – Chris Rogers Mar 04 '15 at 02:49
  • Issue i face is that when i download pagesource and get data than if that website is in other language than my pagesource is not getting those values – Rush.2707 Dec 16 '16 at 09:31
42

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);
John Smith
  • 7,243
  • 6
  • 49
  • 61
Diego Jancic
  • 7,280
  • 7
  • 52
  • 80
34

The newest, most recent, up-to-date answer
This post is really old (it was 7 years old when I answered it), so no one of the other answers used the new and recommended way, which is HttpClient class.


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

longer way

string url = "page url";
HttpClient client = new HttpClient();   // actually only one object should be created by Application
using (HttpResponseMessage response = await client.GetAsync(url))
{
   using (HttpContent content = response.Content)
   {
      string pageContent = await content.ReadAsStringAsync();
   }
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
17

You can get the HTML source with:

var html = new System.Net.WebClient().DownloadString(siteUrl)
John Smith
  • 7,243
  • 6
  • 49
  • 61
Xenon
  • 815
  • 11
  • 26
11

@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

Community
  • 1
  • 1
Xilmiki
  • 1,453
  • 15
  • 22
  • Please be careful when using DownloadString because it breaks the encoding if the website is not using UTF-8. Use instead DownloadData method and handle the encoding part too. – Alexandru Dicu May 13 '21 at 06:40