在按钮单击上查看网页的页面源

时间:2012-12-03 07:22:56

标签: c# asp.net

我正在创建一个应用程序,我想在按钮点击的任何网页的“查看来源”提供设施。用户只会输入URL并将获得该页面的来源。我还想显示内容,样式表,该网页的图片。我希望得到所有这些并根据我的格式在我的asp.net页面中显示。请帮助....

2 个答案:

答案 0 :(得分:2)

您可以在asp.net中使用 web client

private void Button1_Click(object sender, System.EventArgs e)
{
WebClient webClient = new WebClient(); 
const string strUrl = "http://www.yahoo.com/"; 
byte[] reqHTML; 
reqHTML = webClient.DownloadData(strUrl); 
UTF8Encoding objUTF8 = new UTF8Encoding(); 
lblWebpage.Text = objUTF8.GetString(reqHTML); 

}

在这里,您可以在strUrlread more中传递您的网页网址 如果你想使用javascript,那么read this

答案 1 :(得分:1)

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