Skip to content

Instantly share code, notes, and snippets.

@SpringMT
Forked from t-mat/poco-https.cpp
Created October 16, 2017 02:10
Show Gist options
  • Save SpringMT/31a29537f88b82fb0f0413614652d0ca to your computer and use it in GitHub Desktop.
Save SpringMT/31a29537f88b82fb0f0413614652d0ca to your computer and use it in GitHub Desktop.
POCO : https example
#include "Poco/Exception.h"
#include "Poco/StreamCopier.h"
#include "Poco/URI.h"
#include "Poco/URIStreamOpener.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/HTTPSStreamFactory.h"
#include "Poco/Net/AcceptCertificateHandler.h"
#include "Poco/Net/InvalidCertificateHandler.h"
#include "Poco/Net/Context.h"
#include "Poco/Net/SSLManager.h"
#pragma comment(lib, "Crypt32.lib")
#include <memory>
#include <iostream>
std::string getHttpContent(const std::string& url) {
std::string content {};
try {
auto& opener = Poco::URIStreamOpener::defaultOpener();
auto uri = Poco::URI { url };
auto is = std::shared_ptr<std::istream> { opener.open(uri) };
Poco::StreamCopier::copyToString(*(is.get()), content);
} catch (Poco::Exception& e) {
std::cerr << e.displayText() << std::endl;
}
return content;
}
int main(int argc, char** argv) {
Poco::Net::HTTPStreamFactory::registerFactory();
Poco::Net::HTTPSStreamFactory::registerFactory();
// http://stackoverflow.com/questions/18315472/https-request-in-c-using-poco
Poco::Net::initializeSSL();
Poco::Net::SSLManager::InvalidCertificateHandlerPtr ptrHandler ( new Poco::Net::AcceptCertificateHandler(false) );
Poco::Net::Context::Ptr ptrContext ( new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "") );
Poco::Net::SSLManager::instance().initializeClient(0, ptrHandler, ptrContext);
const auto url = "https://github.com/";
const auto content = getHttpContent(url);
std::cout << content << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment