Created
October 23, 2014 16:39
-
-
Save t-mat/5c1b68d56179d7af7e98 to your computer and use it in GitHub Desktop.
POCO : https example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
This sample is out of date: the #include statements are not in line with the structure of 1.8.0.1, (Net/NetSSL_OpenSSL, etc.). Also, some header files are present in different folders (e.g. Exception.h is present in Foundation and in Redis).
Please update.