不,你不不需要Application對象。這裏是一個全功能的例子:
$ httpsget https://httpbin.org/user-agent
{
"user-agent": "Poco HTTPSClientSession"
}
代碼:
#include "Poco/StreamCopier.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/SharedPtr.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/KeyConsoleHandler.h"
#include "Poco/Net/ConsoleCertificateHandler.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include <memory>
#include <iostream>
using namespace Poco;
using namespace Poco::Net;
class SSLInitializer {
public:
SSLInitializer() { Poco::Net::initializeSSL(); }
~SSLInitializer() { Poco::Net::uninitializeSSL(); }
};
int main(int argc, char** argv)
{
SSLInitializer sslInitializer;
SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler(false);
Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", "rootcert.pem", Context::VERIFY_STRICT, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
SSLManager::instance().initializeClient(0, ptrCert, ptrContext);
try
{
if (argc > 1)
{
URI uri(argv[1]);
HTTPSClientSession s(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPath());
request.set("user-agent", "Poco HTTPSClientSession");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
StreamCopier::copyStream(rs, std::cout);
}
}
catch (Exception& ex)
{
std::cout << ex.displayText() << std::endl;
return 1;
}
return 0;
}
語境:: VERIFY_NONE,「客戶:如果不使用匿名密碼(默認禁用),服務器將發送,這將是一個證書檢查,但檢查的結果將被忽略。「這不是基本上打敗證書的觀點嗎? – proteneer
保護者的好處。更改爲verificationMode = Context :: VERIFY_STRICT和loadDefaultCAs = true似乎是更理智的默認值。 – thomasa88
除了'VERIFY_NONE'之外的任何東西看起來確實比'VERIFY_NONE'更安全,但是一些主機_沒有有效的證書... :-( –