How to get the contents of a web page in C#?

SheoNarayan
Posted by SheoNarayan under C# category on | Views : 7941
Lets say you have a requirement where you need to get the actual code of a webpage into C#, you can do that easily with following code.

            string webPageToGet = "http://www.dotnetfunda.com/";
using (WebClient webClient = new WebClient())
{
string response = webClient.DownloadString(webPageToGet); // downloads the string and save into the response variables
Response.Write(response); // write the webpage contents
}


The above code will get the actual html code of the dotnetfunda.com and save into response variable. You can either write it or use in whatever way you want to use.

Comments or Responses

Login to post response