C# Code Snippet - Download HTML Web Page
C# Code Snippet - Download HTML Web Page
This .Net C# code snippet download web page HTML source contents. To use this function simply provide the URL of the web page you like to download. This function read the web page contents and returns HTML source code as a string.
/// <summary> /// Function to download HTML web page /// </summary> /// <param name="_URL">URL address to download web page</param> /// <returns>HTML contents as a string</returns> public string DownloadHTMLPage(string _URL) { string _PageContent = null; try { // Open a connection System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL); // You can also specify additional header values like the user agent or the referer: (Optional) _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; _HttpWebRequest.Referer = "http://www.google.com/"; // set timeout for 10 seconds (Optional) _HttpWebRequest.Timeout = 10000; // Request response: System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse(); // Open data stream: System.IO.Stream _WebStream = _WebResponse.GetResponseStream(); // Create reader object: System.IO.StreamReader _StreamReader = new System.IO.StreamReader(_WebStream); // Read the entire stream content: _PageContent = _StreamReader.ReadToEnd(); // Cleanup _StreamReader.Close(); _WebStream.Close(); _WebResponse.Close(); } catch (Exception _Exception) { // Error Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); return null; } return _PageContent; }
Here is a simple example showing how to use above function (DownloadHTMLPage) to download web page HTML contents and show it in a RichTextBox.
string _WebContents = null; // Read HTML contents from a web page _WebContents = DownloadHTMLPage(@"http://www.digitalcoding.com/"); // Show HTML contents in RichTextBox if (_WebContents != null) richTextBox1.Text = _WebContents;
C# Keywords Used:
- HttpWebRequest
- WebResponse
- Stream
- StreamReader
- ExecuteReader
- Exception
Code Snippet Information:
- Applies To: .Net, C#, CLI, HTML, HttpWebRequest, WebResponse, StreamReader, Data Mining
- Programming Language : C# (C-Sharp)
External Resources: