在C#中如何获取给定网址的HTML源代码?
WebClient
类下载文件。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");
}
基本上:
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);
HttpClient
类。
HttpClient
被认为是新的 API,应该取代旧的 API(WebClient
和 WebRequest
)
HttpClient client = new HttpClient(); // actually only one object should be created by Application
string page = await client.GetStringAsync("page URL here");
更长的方式
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();
}
}
HttpClient
比使用WebClient
快得多。 - ToniCancellationToken
的重载。我已经更新了答案,感谢您的提醒。 - Hakan Fıstık您可以使用以下方法获取HTML源代码:
var html = new System.Net.WebClient().DownloadString(siteUrl)
WebClient
吗? - J D@cms 的方法是最近在微软网站上推荐的,但我曾经遇到一个很难解决的问题,在这里发布所有的解决方案!
问题:
如果您使用像这样的URL:www.somesite.it/?p=1500
,有时会出现内部服务器错误(500),即使在Web浏览器中这个www.somesite.it/?p=1500
也能正常工作。
解决方案: 您需要移除参数,有效代码如下:
using System.Net;
//...
using (WebClient client = new WebClient ())
{
client.QueryString.Add("p", "1500"); //add parameters
string htmlCode = client.DownloadString("www.somesite.it");
//...
}