Skip to content

How to get the innerHTML(source) of a web page using c#?

venkatesh

Asked Jan 2, 2009, 2:51 AM EST1 answer
Share
LinkedInX

Hi all,

As of to my project requirement i have to read the source of a webpage and i have to store the source in a txt document.As i have to retrieve some values of the page from its source and display in my website.And the above functionality has to done in c#.So, anybody can please help me in the above concept can reply back.

                Thanks in advance

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    You'll need to create a web request, and make a request out to the internet to fetch the website.  This is done by calling "GetResponse()", which returns a WebResponse object, which you will cast into an HttpWebResponse, and then call GetResponseStream() on.  The result of GetResponseStream(), will be a stream object, which you can then use to instantiate a StreamReader, and read the text. 

    To write the text out to a file, call File.WriteAllText.

    Here's an example:

    Java
    static void WriteSourceToFile(string url, string filename)
    {
        WebRequest request = HttpWebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string documentText = reader.ReadToEnd();
        reader.Close();
        File.WriteAllText(filename, documentText);
    }
    0