Jaswinder
Jaswinder

Reputation: 51

View Page source of web page on button click

I am making a app on which I want to provide a facility to "VIEW SOURCE" of any webpage on button click.User will only enter URL and will get source of that page.I also want to show contents,stylesheets,images of that webpage .I want to get all these and show in my asp.net pages according to my formats.Please Help....

Upvotes: 0

Views: 2070

Answers (2)

sajanyamaha
sajanyamaha

Reputation: 3198

The WebClient class will do what you want:

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

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

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

Upvotes: 1

Buzz
Buzz

Reputation: 6330

you can use web client in asp.net

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

}

here you can pass your page url in strUrl , read more
if you want to use javascript then read this

Upvotes: 2

Related Questions