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 main.cpp Headers
The IDE automatically generates a source file called main.cpp. Select that source file and add the following declarations and preprocessor directives below to the file:
#include#include #include #include #include #include #include "Internet.hpp" using std::cin; using std::cout; using std::endl; using std::string; using std::auto_ptr; using std::abort; using namespace Internet;
We have included all the headers needed by the main.cpp file to implement this program. You may wonder why include the headers "istream" and "ostream" if I include "iostream" anyway. The answer is simple. According to the reference book written by Ray Lischner entitled C++ In A Nutshell, it states "Many C++ programmers assume that
As you can see the include directives listed also contain a header called "Internet.hpp" and the declarations listed contain a namespace called "Internet". So before we go on with writing the logic for main() we will write the code that is contained inside the Internet namespace first. Save the main.cpp file by going to File > Save and continue to the next topic.
Explanation of Internet namespace
The "Internet" namespace is intended to be a namespace that contains reusable internet related code. It is a custom namespace that we will create ourselves. There is 1 class and 4 functions that we will define in the namespace. You will learn about each of the elements in the namespace throughout the course of this article.