c++ - 使用 boost asio 的 HTTPS POST 请求

标签 c++ boost https boost-asio

我正在看this example用于发出 HTTP POST 请求。

我对发出 HTTPS POST 请求感兴趣。如何提供 .crt 和 .key 文件的位置?有没有可能也显示异常处理的示例?

最佳答案

这是简单 POST 请求的基础。

  1. 如果您定义 DEMO_USING_SSL,您将获得 SSL,否则不会获得 SSL
  2. 线路

    ctx.set_default_verify_paths();
    

    设置验证路径,以便您应该(通常/通常)选择受信任的系统根证书。或者还有

    ctx.add_verify_path(...);
    ctx.add_certificate_authority(...);
    

    请务必查看man c_rehash如果您打算使用(强大的)add_verify_path 方法。

  3. 现在,您甚至可以禁用/调整证书验证。或者,实际上,如您在 OP 中提到的那样添加错误处理:

    ctx.set_verify_mode(...);
    ctx.set_verify_depth(...);
    
    ctx.set_password_callback(...); // for passphrases of private keys
    ctx.set_verify_callback(VerifyCallback);
    

    在下面的示例中,我展示了后者

#define DEMO_USING_SSL
#define BOOST_ASIO_ENABLE_HANDLER_TRACKING

#include <iostream>
#include <iomanip>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

class client
{
public:
  client(boost::asio::io_service& io_service,
      boost::asio::ssl::context& context,
      boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
    : socket_(io_service
#ifdef DEMO_USING_SSL
            , context)
  {
    socket_.set_verify_mode(boost::asio::ssl::verify_peer);
    socket_.set_verify_callback(
        boost::bind(&client::verify_certificate, this, _1, _2));
#else
            )
  {
      (void) context;
#endif

    boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
        boost::bind(&client::handle_connect, this,
          boost::asio::placeholders::error));
  }

  bool verify_certificate(bool preverified,
      boost::asio::ssl::verify_context& ctx)
  {
      // The verify callback can be used to check whether the certificate that is
      // being presented is valid for the peer. For example, RFC 2818 describes
      // the steps involved in doing this for HTTPS. Consult the OpenSSL
      // documentation for more details. Note that the callback is called once
      // for each certificate in the certificate chain, starting from the root
      // certificate authority.

      // In this example we will simply print the certificate's subject name.
      char subject_name[256];
      X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
      X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
      std::cout << "Verifying " << subject_name << "\n";

      return preverified;
  }

  void handle_connect(const boost::system::error_code& error)
  {
#ifdef DEMO_USING_SSL
      if (!error)
      {
          socket_.async_handshake(boost::asio::ssl::stream_base::client,
                  boost::bind(&client::handle_handshake, this,
                      boost::asio::placeholders::error));
      }
      else
      {
          std::cout << "Connect failed: " << error.message() << "\n";
      }
#else
      handle_handshake(error);
#endif
  }

  void handle_handshake(const boost::system::error_code& error)
  {
      if (!error)
      {
          std::cout << "Enter message: ";
          static char const raw[] = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n";

          static_assert(sizeof(raw)<=sizeof(request_), "too large");

          size_t request_length = strlen(raw);
          std::copy(raw, raw+request_length, request_);

          {
              // used this for debugging:
              std::ostream hexos(std::cout.rdbuf());
              for(auto it = raw; it != raw+request_length; ++it)
                  hexos << std::hex << std::setw(2) << std::setfill('0') << std::showbase << ((short unsigned) *it) << " ";
              std::cout << "\n";
          }

          boost::asio::async_write(socket_,
                  boost::asio::buffer(request_, request_length),
                  boost::bind(&client::handle_write, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
      }
      else
      {
          std::cout << "Handshake failed: " << error.message() << "\n";
      }
  }

  void handle_write(const boost::system::error_code& error,
      size_t /*bytes_transferred*/)
  {
      if (!error)
      {
          std::cout << "starting read loop\n";
          boost::asio::async_read_until(socket_,
                  //boost::asio::buffer(reply_, sizeof(reply_)),
                  reply_, '\n',
                  boost::bind(&client::handle_read, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
      }
      else
      {
          std::cout << "Write failed: " << error.message() << "\n";
      }
  }

  void handle_read(const boost::system::error_code& error, size_t /*bytes_transferred*/)
  {
      if (!error)
      {
          std::cout << "Reply: " << &reply_ << "\n";
      }
      else
      {
          std::cout << "Read failed: " << error.message() << "\n";
      }
  }

private:
#ifdef DEMO_USING_SSL
  boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
#else
  boost::asio::ip::tcp::socket socket_;
#endif
  char request_[1024];
  boost::asio::streambuf reply_;
};

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 3)
        {
            std::cerr << "Usage: client <host> <port>\n";
            return 1;
        }

        boost::asio::io_service io_service;

        boost::asio::ip::tcp::resolver resolver(io_service);
        boost::asio::ip::tcp::resolver::query query(argv[1], argv[2]);
        boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);

        boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
        ctx.set_default_verify_paths();

        client c(io_service, ctx, iterator);

        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

关于c++ - 使用 boost asio 的 HTTPS POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21716668/

相关文章:

c++ - 同时为 booster 构建 32 位和 64 位库?

c++ - boost 元组性能

javascript - 我可以创建一个同时适用于 HTTP 和 HTTPS 的 javascript 文件吗

apache - htaccess 重定向 http ://and http://www to https://that plays nice with subdomains

c++ - 如何在圆圈内绘制图像?

c++ - 如何从 GLSL 程序中获取 uvec4 输出

C++ 如何运行 2 boost :asio: io_context at the same time

Android 防止 SSL 的中间人攻击

c++ - c_str() 的生命周期是否在 g++ 4.8.4 和 g++ 5.3.1 之间发生了变化?

c++ - 如何在 c++(0x) 中使用多个返回值进行初始化