【问题标题】:HTTPS request in c++ using Poco使用 Poco 的 c++ 中的 HTTPS 请求
【发布时间】:2013-08-21 08:21:55
【问题描述】:

我正在尝试使用 Poco 库(版本 poco-1.4.6p1-all)在 C++ 中编写客户端应用程序并在 Visual Studio 2010 中编译,它将 HTTPS 请求发送到具有自写证书的服务器。我有一个错误,因为证书无法识别:

First-chance exception at 0x76e8c41f in httprequest.exe: Microsoft C++ exception: Poco::Net::SSLException at memory location 0x0044ed38..

我尝试更改在库中编写的验证函数(在 X509Certificate.h 中),以便它们始终返回 true 并重建库。同样的错误。

代码如下:

try{
    const Poco::URI uri("https://www.theServer.com");
    Poco::Net::Context::Ptr context =
        new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "",
        "","",Poco::Net::Context::VERIFY_RELAXED,
        9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

    Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> pAcceptCertHandler = new Poco::Net::AcceptCertificateHandler(true);
    Poco::Net::SSLManager::instance().initializeClient(NULL, pAcceptCertHandler, context);

    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context );
    Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, "" );
    req.setContentType("application/x-javascript; charset=utf-8\r\n");
    req.setKeepAlive(true);

    Poco::Net::HTTPBasicCredentials cred("lala@lala.lala", "lala");
    cred.authenticate(req);
    session.sendRequest(req);
    Poco::Net::HTTPResponse res;
    std::istream& rs = session.receiveResponse(res);
    std::string resp;

    std::vector<Poco::Net::HTTPCookie> cookies;
    res.getCookies( cookies );
    res.write(std::cout);
}
catch( const Poco::Net::SSLException& e )
{
    std::cerr << e.what() << ": " << e.message() << std::endl;
}
catch( const std::exception& e )
{
    std::cerr << e.what() << std::endl;;
}

谢谢!

【问题讨论】:

  • 我意识到无论我尝试连接的服务器如何,都会引发此异常。会不会是因为 OpenSSL 安装不正确?
  • 我找到了你的答案。问题是我实际上并没有获得证书。它的工作原理是这样的:

标签: c++ ssl https ssl-certificate poco-libraries


【解决方案1】:

我找到了答案。我真的没有拿到证书。它的工作原理是这样的:

 try{
    Poco::Net::initializeSSL();
    Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrHandler = new AcceptCertificateHandler(false);
    Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    SSLManager::instance().initializeClient(0, ptrHandler, ptrContext);

    Poco::Net::SocketAddress address("www.server.com:443");
    Poco::Net::SecureStreamSocket socket(address);
    if (socket.havePeerCertificate())
    {
        X509Certificate cert = socket.peerCertificate();
        std::cout<<cert.issuerName()<<"\n"; 
    }
    else
    {
        std::cout<<"No certificate";
    }

}catch (Poco::Exception& e) {
    std::cout << "Error: " << e.displayText() << "\n";
    return -1;
}

【讨论】:

  • 这不只是通过使用 AcceptCertificateHandler 绕过证书(它总是接受证书,即使验证失败)?
  • 是的,当时我们暂时使用了这个解决方案,因为我们的证书有问题。
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 2014-11-19
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多