Programming an HTML Downloader in C++
Written By: Alwyn Malachi Berkeley
- 25 Sep 2006 -
Description: This program requests the source code (HTML) of a webpage and then gives the user multiple options for viewing the web server's response. We will use the Winsock API to manipulate sockets on the system, and create reusable code along the way.
- Creating the Project
- main.cpp Headers and the Internet Namespace
- The Webpage Class
- The Internet Namespace's Functions
- GetDomain
- Custom_itoa
- GetWebpage
- SaveFile
- Internet.hpp and Writing main.cpp
- Compile and Conclusion
Defining the Internet namespace's four functions
Now, if you recall there are four other functions that are defined by the Internet namespace. The name of those four functions are GetDomain(), Custom_itoa, GetWebpage(), and SaveFile(). To define these functions go to File > New > Source File once again, and this time, fill it with the following code below:
#ifndef HELPER_FUNCTIONS_HPP #define HELPER_FUNCTIONS_HPP #include#include #include "BasicWebpage.hpp" namespace Internet { const char* GetDomain(const std::string &strWebpageURLParam); const char* Custom_itoa(int intParam); std::auto_ptr GetWebpage(const std::string &strWebpageURLParam); const bool SaveFile(const std::string &strFileNameParam, const std::string &strSaveDataParam); } #endif
Simple as that, we are done defining the four functions. Save the source file under the name "InternetHelperFunctions.hpp".
Now let us take a look at the function implementations. Note that I will skip past outlining what headers are needed and skip by explaining how to declare a namespace because that information is too trivial. You can take a gander at my source code for those minute details. For now let's press on to the meat and potatoes.