如何使用ASP.NET获取网页的内容?我需要编写一个程序来获取网页的HTML并将其存储到字符串变量中。如何获取网页内容并将其保存到字符串变量中
61
A
回答
94
可以使用WebClient
WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.gooogle.com");
7
Webclient client = new Webclient();
string content = client.DownloadString(url);
通过你想要得到的页面的URL。你可以使用htmlagilitypack来分析结果。
62
我已经与Webclient.Downloadstring之前遇到的问题。使用WebClient.DownloadString
WebRequest request = WebRequest.Create("http://www.google.com");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
19
我建议不:如果你这样做,你可以试试这个。这是因为(至少在.NET 3.5中)DownloadString不够聪明,可以使用/删除BOM,如果它存在。这会导致BOM(
)在返回UTF-8数据时(至少没有字符集)错误地作为字符串的一部分显示 - ick!
相反,这种轻微变化将正确地工作的材料明细表:
string ReadTextFromUrl(string url) {
// WebClient is still convenient
// Assume UTF8, but detect BOM - could also honor response charset I suppose
using (var client = new WebClient())
using (var stream = client.OpenRead(url))
using (var textReader = new StreamReader(stream, Encoding.UTF8, true)) {
return textReader.ReadToEnd();
}
}
不幸的是DownloadString(截至.NET 3.5)是不够聪明与物料清单的工作。我在答复中加入了一个替代方案。 – user2246674 2013-05-04 00:13:56
没有投票,因为没有使用(WebClient客户端=新的WebClient()){} :) – 2013-07-15 04:24:01