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.

  1. Creating the Project
  2. main.cpp Headers and the Internet Namespace
  3. The Webpage Class
  4. The Internet Namespace's Functions
  5. GetDomain
  6. Custom_itoa
  7. GetWebpage
  8. SaveFile
  9. Internet.hpp and Writing main.cpp
  10. 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 automatically #includes and , but the standard does not guarantee that behavior. Always #include every header you need…" This is why we explicitly declare each header we need, the result is more portable code between compilers. As for the series of lines beginning with "using std::", that is merely my programming style. I like programmers to take a glance at the top of files and instantly know what functions/objects from the std namespace they ought to be familiar with in order to read the following source code. If you prefer you can alternatively write "using namespace std;" in their place.

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.

<< Previous

Next >>