Download a web page

C-Sharp Code to download a web page

Below code allow to download a web page, after downloading you can easily save it to database or on file system.

/// <summary>
/// Returns the content of a given web adress as string.
/// </summary>
/// <param name="Url">URL of the webpage</param>
/// <returns>Website content</returns>
public string DownloadWebPage(string Url)
    // Open a connection
    HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);
    // You can also specify additional header values like 
    // the user agent or the referer:
    WebRequestObject.UserAgent	= ".NET Framework/2.0";
    WebRequestObject.Referer	= "";
    // Request response:
    WebResponse Response = WebRequestObject.GetResponse();
    // Open data stream:
    Stream WebStream = Response.GetResponseStream();
    // Create reader object:
    StreamReader Reader = new StreamReader(WebStream);
    // Read the entire stream content:
    string PageContent = Reader.ReadToEnd();
    // Cleanup
    return PageContent;