Download Website Source with C++ HttpWebRequests

c++ http web requestswrapper

 

As you may already know, there are some C++ libraries to help you interact with Web Application from your C++ Project. One of the most popular is libCURL and it is the most powerfully. But, if your project is smaller enough, you can use my C++ HttpWebRequests Wrapper to download a source page, for example.

How to do this?

HttpWebRequestsCPP is a C++ Wrapper to help you interact with Web applications. Is is a very very basic implementation of HttpWebRequests from .NET. It support all types of proxy (HTTP, HTTPS, SOCKS4, SOCKS5). Is has also a CookieContainer if you’re working with cookies.

The wrapper can be found here:

https://github.com/HttpWebRequestsCPP

How to use?

The main advantage for this wrapper is that it is header only.

All you have to do is to download HttpWebRequests.hpp and HttpWebRequests.hpp then include .hpp file into your project.

Example

Here is an example to understand how to use HttpWebRequestsCPP and download a page source code:

#include "HttpWebRequests.hpp"
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    HttpWebRequest req;
    req.setUrl("checkip.dyndns.com");

    req.proxy.enable = true;
    req.proxy.proxy_type = PROXY_SOCKS5;
    req.proxy.ipOrHostname = "xx.xx.xx.xx";
    req.proxy.port = 3128;

    req.headers.httpMethod =  GET;
    req.headers.keepCookies = true;
    req.headers.cookieContainer = "";
    req.headers.userAgent = "Easy HttpWebRequests 0.1/ www.howtofix.pro";
    req.headers.connection = "close";
    req.headers.postContent = "login=1&type=submit";
    //req.headers.custom = "If-Modified-Since: *\r\n"; //Don't forget about \r\n.

    std::string document, header;

    if( req.downloadDocument(header, document) )
    {
        cout << "HEADER: " << endl << header << endl << endl;
        cout << "DOCUMENT: " << endl << document  << endl;
    }
    else
    {
        cout << "Failed to download document!" << endl;
        cout << "Error: " << req.getLastError() << endl;
    }
    return 0;
}

Preliminary info

Is is based on Winsock2. So this will work under Windows only!
It did not support SSL, yet!

Related posts:

Got something to say? Go for it!