C# 如何下载C语言中的HTML源代码#
如何在C#中获取给定网址的HTML源代码?您可以使用以下工具下载文件: 基本上:C# 如何下载C语言中的HTML源代码#,c#,C#,如何在C#中获取给定网址的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 (Str
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);
@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");
//...
}
您可以通过以下方式获得:
var html = new System.Net.WebClient().DownloadString(siteUrl)
最新、最新、最新的答案
这篇文章真的很老了(我回答的时候已经7年了),所以其他答案中没有一个使用了新的推荐方式,那就是
HttpClient
class
HttpClient
被认为是新的API,它应该取代旧的API(WebClient
和WebRequest
)
有关如何使用HttpClient
类(特别是在异步情况下)的更多信息,您可以参考
注意1:如果要使用异步/等待
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 result = await content.ReadAsStringAsync();
}
}
注2:如果使用C#8功能
string url = "page url";
HttpClient client = new HttpClient();
using HttpResponseMessage response = await client.GetAsync(url);
using HttpContent content = response.Content;
string result = await content.ReadAsStringAsync();
应该注意:如果需要更多的控制,请查看HttpWebRequest类(例如,能够指定身份验证)。是的,HttpWebRequest提供了更多的控制,尽管您可以使用client.UploadData(URI字符串,“POST”,postParamsByteArray)通过WebClient执行POST请求;抓住WebException的这一点不是很明智吗?也许这是假设。需要使用此方法捕获任何其他异常或错误吗?@JohnWasham-是的,在这里捕获异常是谨慎的。不过,谢天谢地,大多数StackOverflow的受访者都尽可能地保持示例代码的清晰和简洁。让示例代码更接近“现实生活”只会增加噪音。我面临的问题是,当我下载pagesource并获取数据时,若该网站使用的语言不是我的pagesource,那个么它就无法获取这些值—又短又甜!我读了乔·阿尔巴哈里的例子后发现了你的建议。LINQPad>Help>What's New,并搜索Cache.var html=New System.Net.WebClient().DownloadString(siteUrl);//需要更新你的客户!这是否是
Dispose
网络客户端的WebClient
?建议:等待异步方法。@Maarten下面的链接显示了如何将其与async一起使用/Wait在这里使用异步调用的任何优势?我认为总是建议尽可能使用async,因为这可能需要时间,您不想用Wait()调用阻塞线程谢谢。使用HttpClient
比WebClient
快得多。使用DownloadString时请小心,因为如果网站不使用UTF-8,它会破坏编码。使用DownloadData方法并处理编码部分。
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 result = await content.ReadAsStringAsync();
}
}
string url = "page url";
HttpClient client = new HttpClient();
using HttpResponseMessage response = await client.GetAsync(url);
using HttpContent content = response.Content;
string result = await content.ReadAsStringAsync();