C++/CLI Code Snippet - Download HTML Web Page
C++/CLI Code Snippet - Download HTML Web Page
This .Net C++/CLI 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 from URL /// </summary> /// <param name="_URL">URL address to download web page</param> /// <returns>HTML contents as a string</returns> System::String ^DownloadHTMLPage(System::String ^_URL) { System::String ^_PageContent = nullptr; try { // Open a connection System::Net::HttpWebRequest ^_HttpWebRequest = safe_cast<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 = gcnew 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 nullptr; } 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.
System::String ^_WebContents = nullptr; // Read HTML contents from a web page _WebContents = DownloadHTMLPage("http://www.digitalcoding.com/"); // Show HTML contents in RichTextBox if (_WebContents != nullptr) richTextBox1->Text = _WebContents;
C++/CLI 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++/CLI
External Resources:
Jonnism :: July 12-2010 :: 11:41 AM
This is basically the .net one. If you need C++ on win32 without the overhead or dependency on CLI, then you can use something like HINTERNET.
Something like this
http://www.codequests.com/project.aspx?projectid=HInternetWGet
A couple of conveniences are obviously not there and you'll have to add some things manually.