如何在 POCO C++ 库中正确使用 OpenSSL

2021-12-26 00:00:00 https openssl c++ poco-libraries

根据POCO助手中的规范:

According to the Specification in POCO assistant:

初始化 NetSSL 库,以及底层的 OpenSSL库,通过调用 Poco::Crypto::OpenSSLInitializer::initialize().应在使用 NetSSL 库中的任何类之前调用??.NetSSL 将自动初始化,通过Poco::Crypto::OpenSSLInitializer 实例或类似机制创建 Context 或 SSLManager 实例时.但是,建议调用 initializeSSL()在任何情况下在应用程序启动时.

Initialize the NetSSL library, as well as the underlying OpenSSL libraries, by calling Poco::Crypto::OpenSSLInitializer::initialize(). Should be called before using any class from the NetSSL library. The NetSSL will be initialized automatically, through Poco::Crypto::OpenSSLInitializer instances or similar mechanisms when creating Context or SSLManager instances. However, it is recommended to call initializeSSL() in any case at application startup.

当我想使用 HTTPSClientSession 时,是否必须先构造一个 Application 对象?如何在客户端中使用它?有大佬可以告诉我吗?非常感谢!

When I want to use HTTPSClientSession,do I have to construct an Application object first? How can I use it in Client? Any guy can tell me ?Thank you very much!

推荐答案

以Net/samples/httpget为例,我们将httpget/复制为一个新的httpsget目录:

Let's take Net/samples/httpget as an example, let's copy httpget/ as a new httpsget directory:

  1. 打开 Makefile,将PocoNetSSL"添加到 target_libs
  2. 将HTTPClientSession"替换为HTTPSClientSession"
  3. 您需要为 SSL 使用创建 Poco::Net::Context
  4. replace 'HTTPClientSession session(uri.getHost(), uri.getPort());'有以下两行:

const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

总结:

  1. 将 PocoNetSSL 添加为 lib_depends
  2. 使用 Poco::Net::Context 和 HTTPSClientSession

相关文章