gpt4 book ai didi

c# - 如何在 ASP.NET C# 中获取网页源代码?

转载 作者:太空狗 更新时间:2023-10-30 00:01:03 25 4
gpt4 key购买 nike

如何在 C# ASP.NET 中获取页面的 HTML 代码?

示例:http://google.com

如何通过 ASP.NET C# 获取此 HTML 代码?

最佳答案

WebClient类将做你想做的事:

string address = "http://stackoverflow.com/";   

using (WebClient wc = new WebClient())
{
string content = wc.DownloadString(address);
}

如评论中所述,您可能更喜欢使用 DownloadString 的异步版本以避免阻塞:

string address = "http://stackoverflow.com/";

using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(DownloadCompleted);
wc.DownloadStringAsync(new Uri(address));
}

// ...

void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if ((e.Error == null) && !e.Cancelled)
{
string content = e.Result;
}
}

关于c# - 如何在 ASP.NET C# 中获取网页源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1820991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com