Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the source of some website from asp.net code

Tags:

c#

asp.net-2.0

Is there any way that I could get the source of a website (as a string preferably), let's say www.google.com, from some c# code inside code behind of asp.net website?

edit: of course i mean html code - in every browser you can view it using "view source" in context menu.

like image 782
agnieszka Avatar asked Dec 13 '22 05:12

agnieszka


2 Answers

Assuming you want to retrieve the html:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        using (Stream stream = client.OpenRead("http://www.google.com"))
        using (StreamReader reader = new StreamReader(stream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}
like image 101
Darin Dimitrov Avatar answered Dec 31 '22 19:12

Darin Dimitrov


For C#, I prefer to use HttpWebRequest over WebClient because you can have more option in the future like having GET/POST parameter, using Cookies, etc.

You can have a shortest explication at MSDN.

Here is the example from MSDN:

        // Create a new HttpWebRequest object.
        HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://www.contoso.com/example.aspx");    

        // Set the ContentType property. 
        request.ContentType="application/x-www-form-urlencoded";
        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";
        // Start the asynchronous operation.    
        request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);    

        // Keep the main thread from continuing while the asynchronous
        // operation completes. A real world application
        // could do something useful such as updating its user interface. 
        allDone.WaitOne();

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object.
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse.
        response.Close();
like image 31
Patrick Desjardins Avatar answered Dec 31 '22 19:12

Patrick Desjardins