Download a web page
This example shows how to download and store the content of a web page in a string.
/// <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; }
Author:
Jonas John
License:
Public domain
Language:
C#
Created:
10/30/2007
Updated:
10/30/2007
Tags:
http, network
string PageContent = DownloadWebPage("http://www.jonasjohn.de/");
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
/// <summary>
/// Returns the content of a given web adress as string.
/// from: http://www.jonasjohn.de/snippets/csharp/download-webpage.htm
/// under public domain
/// </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 = "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)";
webRequestObject.Referer = "http://www.google.com/";
// Will contain the content of the page
string pageContent = string.Empty;
// Request response:
using (WebResponse response = webRequestObject.GetResponse())
// Open stream and create reader object:
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
// Read the entire stream content:
pageContent = reader.ReadToEnd();
}
return pageContent;
}
Writer.Write(Reader.ReadToEnd());
Then, I gave a zip file as input.
Something _is_ downloaded, but the ZIP file is corrupt.
If you can get the content from the www.pindigital.com , then will rate you a good asp.net developer.