How can I download HTML source in C#

2019-01-01 10:52发布

问题:

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

回答1:

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


回答2:

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


回答3:

You can get it with:

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


回答4:

This post is really old (it\'s 7 years old when I am answering it), so no one of the other solutions 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)

string url = \"page url\";

using (HttpClient client = new HttpClient())
{
    using (HttpResponseMessage response = client.GetAsync(url).Result)
    {
        using (HttpContent content = response.Content)
        {
            string result = content.ReadAsStringAsync().Result;
        }
    }
}

for more information about how to use the HttpClient class (especially in async cases), you can refer this question



回答5:

@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 (yes is easy), 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



标签: c#