如何在ASP.NET C#中获取网页源?

2024-01-07

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

例子:http://google.com

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


The WebClient http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx类将做你想做的事:

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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在ASP.NET C#中获取网页源? 的相关文章

随机推荐