HTTP HTTPS GET Requests with Poco

The standard Poco GET example has one major deficit, I can only succesfully execute a GET request against a server that supports HTTP. Most servers do not provide their content using HTTP any more. Instead a HTTPS GET request has to be executed.

In order to adjust the Poco HTTP example to use HTTPS, follow the steps outlined in https://stackoverflow.com/questions/10875938/how-to-use-openssl-in-poco-c-library-correctly

The example below is not good code at all whatsoever but it shows how to use the HTTPSClientSession instead of the HTTPClientSession to leverage the HTTPS protocol instead of the HTTP protocol.

#include <iostream>

#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include <Poco/Exception.h>

using namespace std;

using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;

int main(int argc, char** argv) {

  try {

    std::cout << "Hello world2" << std::endl;

    //URI uri("https://www.google.de:80/index.html");
    //URI uri("https://www.play-hookey.com:80/htmltest/");
    //URI uri("http://www.brainjar.com:80/java/host/test.html");

    //http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)

    //URI uri("http://us.imdb.com:80/Title?Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com:443/Title?Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com/find?q=Toy%20Story%20(1995)");
    URI uri("https://www.imdb.com/find?q=Se7en%20(1995)");

    //URI uri("https://www.imdb.com:443/find?q=Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com:443/find");
    //URI uri("https://143.204.95.231:443/find");
    //URI uri("https://www.imdb.com:443");
    //URI uri("https://www.imdb.com");
    //URI uri("www.imdb.com");

    //URI uri("https://stackoverflow.com/");

    //URI uri("https://github.com/");

    // prepare path
    string path(uri.getPathAndQuery());
    if (path.empty())
      path = "/";

    std::cout << "host " << uri.getHost() << " port " << uri.getPort()
        << " path " << path << std::endl;

    HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);

    // HTTP
    //HTTPClientSession session(uri.getHost(), uri.getPort());

    // HTTPS
    const Poco::Net::Context::Ptr context = new Poco::Net::Context(
        Poco::Net::Context::CLIENT_USE, "", "", "",
        Poco::Net::Context::VERIFY_NONE, 9, false,
        "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

    std::cout << "session.sendRequest" << std::endl;
    session.sendRequest(request);

    std::cout << "session.receiveResponse" << std::endl;

    HTTPResponse response;
    std::istream& rs = session.receiveResponse(response);

    std::cout << "session.getStatus" << std::endl;
    std::cout << response.getStatus() << " " << response.getReason()
        << std::endl;

    //StreamCopier::copyStream(rs, std::cout);

    std::string responseAsString(std::istreambuf_iterator<char>(rs), { });

  } catch (Exception &ex) {
    std::cerr << ex.displayText() << std::endl;
    return -1;
  }

  return 0;
}

 

Leave a Reply