Get Web Page Contents in Code with C#

When you are developing an application that needs to access data stored on a web server, you can easily get the contents of a web page with this simple C# function.


The .NET framework provides a rich set of methods to access data stored on the web. First you will have to include the right namespaces:
using System.Text;
using System.Net;
using System.IO;

The HttpWebRequest object allows us to create a request to the URL, and the WebResponse allows us to read the response to the request.

We’ll use a StreamReader object to read the response into a string variable.

Here’s the actual code:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();

In this code sample, the URL variable should contain the URL that you want to get, and the result variable will contain the contents of the web page. You may want to add some error handling as well for a real application.

The Conversation

Follow the reactions below and share your own thoughts.

20 Responses to “Get Web Page Contents in Code with C#”

  1. December 05, 2008 at 9:31 am, sagiv said:

    thank’s a lot!!! it really helped!

    Reply

    • March 25, 2014 at 9:19 am, Aman Kumar said:

      This code is not working it shows an error:- System can not connect with remote machine

      Reply

  2. December 29, 2008 at 5:00 am, luis said:

    Thanks

    Reply

  3. January 05, 2009 at 10:48 pm, bas said:

    thanks,
    just wat i needed

    Reply

  4. June 29, 2009 at 4:32 am, Laotuongrobo said:

    I had do it.. but have a problems : The remote name could not be resolved: ‘http://www.yahoo.com’
    Please help!

    Reply

  5. August 27, 2009 at 12:22 pm, Anonymous said:

    Thanks ! a lot sir

    Reply

  6. November 06, 2009 at 11:12 pm, Cody Cooper said:

    thanks so much

    Reply

  7. April 08, 2010 at 1:33 pm, Anonymous said:

    Thanks. It’s useful for me :)

    Reply

  8. July 14, 2010 at 6:18 pm, Qibo Zhang said:

    I love the code sample! It is simple but very useful!

    How to code if remote server requires usename and password?

    Thanks!

    Reply

  9. February 10, 2011 at 9:07 am, Mtouseef Zafar said:

    nice

    Reply

  10. August 23, 2011 at 3:18 am, Hooma said:

    the webrequest.create work just fine in a console application , but in winodws applications i encounter the error which says :
    The type or namespace name ‘Create’ does not exist in the namespace ‘WebRequest’ (are you missing an assembly reference?)
    can someone help me with that

    Reply

  11. September 22, 2011 at 7:58 am, hamed said:

    hi,
    thanks, but:
    what if you want to extract text content of this URL which don’t contains html tags ?

    Reply

  12. November 05, 2011 at 2:10 am, hosein said:

    hi,
    It was so useful and exactly what I looking for.
    by the way, it’s very optimized code.

    thanks.

    Reply

  13. January 04, 2012 at 5:34 pm, Kevin said:

    Worked great. Thanks!

    Reply

  14. January 22, 2012 at 11:17 am, andres said:

    How I can get per separate each of get/post values ???

    Reply

  15. April 19, 2012 at 4:47 am, sandy said:

    Thanks a lot….

    Reply

  16. May 11, 2012 at 9:00 am, shweta said:

    hey thanks a lot ……just not getting how to thank you…
    you solved the problem completely…god bless you..

    Reply

  17. September 26, 2012 at 12:33 pm, murat said:

    this codes works fine however when the websites updates its data by a scripts, we cannot catch the data. Is there any solution for it?

    Reply

    • March 31, 2013 at 1:19 am, seb said:

      I want ger all content of one web page, including HTML and javascript file. how to get??

      Reply

  18. January 31, 2014 at 11:52 pm, juned said:

    i am getting error in Create method it shows undefined method Create
    in HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urlAddress);

    i have flowed all the steps which you have listed here.

    please help me ASAP…..

    thanks in Advance……………….

    Reply

Leave a Reply