Search code examples
c++httpsessionpoco-libraries

How to use the Poco C++ HTTPSessionFactory


How is the best, simplest way to set up the HTTPSessionFactory to handle both HTTP and HTTPS? I know that I need to use HTTPSessionInstantiators, but I could really use a short and sweet example. Thanks.


Solution

  • You need to register the 'http' and 'https' protocols.

    And for handling 'https', you also need to configure your SSLManager with cert and context, here is a piece of example code:

    //register http and https
    HTTPSessionFactory::defaultFactory().registerProtocol("http", new HTTPSessionInstantiator);
    HTTPSessionFactory::defaultFactory().registerProtocol("https", new HTTPSSessionInstantiator);
    //prepare for SSLManager
    SharedPtr ptrCert = new AcceptCertificateHandler(false);
    const Poco::Net::Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    SSLManager::instance().initializeClient(0, ptrCert, context);
    // now you have the HTTP(S)ClientSession
    HTTPClientSession *session = HTTPSessionFactory::defaultFactory().createClientSession(uri);