Return HTML Page Source From Web URL in C#

Below is a snippet of code showing how to retrieve the page source of a web page. This code will return the HTML source from the home page on this site. It will then load that HTML into the string myPageSource.

using System.Net;
using System.Xml;
using System.IO; 

string url = "http://yahoo.com";

HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";

// make request for web page
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
string myPageSource = myWebSource.ReadToEnd();
myWebResponse.Close();

2 Comments

Add yours

  1. thanks.
    but i want to see page source on page load…

  2. @ozstudio – Maybe PhantomJS? Haven’t tested it yet, but it looks like it might work.
    http://phantomjs.org/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.