Get HTML of a web page using C#.NET


Below i am using a function that takes a web page full url as string and return back HTML of that page as string. You can
use this returned HTML in your code as per your requirement.

Step 1:
First import 2 namespaces that are required by this function to work.

using System.IO;
using System.Net;

Step 2:
Copy & Paste this function in your code behind.

public string GetHTML(string strURL)
{
HttpWebRequest wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method = “GET”;
HttpWebResponse wbrs = (HttpWebResponse)wbrq.GetResponse();
StreamReader sr = new StreamReader(wbrs.GetResponseStream());
strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}

Step 3:
Call this function as per your requirement.

string strHtml=this.GetHTML(“http://www.google.com”);

Happy Programming 🙂

Advertisements

2 thoughts on “Get HTML of a web page using C#.NET

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s