Download a web page

This example shows how to download and store the content of a web page in a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <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    = "http://www.example.com/";
 
    // 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
    Reader.Close();
    WebStream.Close();
    Response.Close();
 
    return PageContent;
}
//Example:
string PageContent = DownloadWebPage("http://www.jonasjohn.de/");
X

Url: http://www.jonasjohn.de/snippets/csharp/download-webpage.htm

Language: C# | User: ShareMySnippets | Created: Oct 16, 2013