加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:34:42 所属栏目:asp.Net 来源:网络整理
导读:如何在C#ASP.NET中获取页面的 HTML代码? 示例:http://google.com 我如何通过ASP.NET C#获取此HTML代码? 解决方法 WebClient 课程会做你想要的: string address = "https://stackoverflow.com/"; using (WebClient wc = new WebClient()){ string conten
如何在C#ASP.NET中获取页面的 HTML代码?

示例:http://google.com

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

解决方法

WebClient课程会做你想要的:

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

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

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

string address = "https://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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读