如何获得在ASP.NET C#中的网页源? [英] How to get the webpage source in ASP.NET C#?

查看:100
本文介绍了如何获得在ASP.NET C#中的网页源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得在C#ASP.NET页面的HTML code?

How can I get the HTML code of a page in C# ASP.NET?

例如: http://google.com

我如何能够通过ASP.NET C#得到这个HTML code?

How I can get this HTML code by ASP.NET C#?

推荐答案

Web客户端 类会做你想要什么:

The WebClient class will do what you want:

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

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

由于在评论中提到,你可能preFER使用 DownloadString 的异步版本,以避免阻塞:

As mentioned in the comments, you might prefer to use the async version of DownloadString to avoid blocking:

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;
    }
}

这篇关于如何获得在ASP.NET C#中的网页源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆