【问题标题】:C++ boost::asio https through proxyC++ boost::asio https 通过代理
【发布时间】:2017-08-10 15:13:50
【问题描述】:

所以我有这段代码可以很好地使用 boost::asio 连接 ssl:

打开了 Fiddler,但我根本看不到这些联系。 从我读到的内容中,我收集到我必须通过代理传递请求。 所以我尝试了所有我能找到的示例和答案,但我无法让它发挥作用。

我得到的最好的结果是握手从未触发回调。

我错过了什么?

void performHTTPRequest(std::string host, std::string path) {
    std::ostream request_stream(&mRequest);
    request_stream << "GET " << path << " HTTP/1.1\r\n";
    request_stream << "Host: " << host << "\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Connection: close\r\n\r\n";
    tcp::resolver::query query(host, "https");
    mResolver.async_resolve(query,
        boost::bind(handle_resolve,
        this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::iterator));
}

void handle_resolve(const boost::system::error_code& err,
        tcp::resolver::iterator endpoint_iterator)
{
    if (!err)
    {
        boost::asio::async_connect(mSocket.lowest_layer(), 
            endpoint_iterator,
            boost::bind(&handle_connect, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_connect(const boost::system::error_code& err)
{
    if (!err)
    {
        mSocket.async_handshake(
            boost::asio::ssl::stream_base::handshake_type::client,
            boost::bind(&handle_handshake, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}


void handle_handshake(const boost::system::error_code& err)
{
    if (!err)
    {
        boost::asio::async_write(mSocket, mRequest,
            boost::bind(&handle_write_request, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_write_request(const boost::system::error_code& err)
{
    if (!err)
    {
        boost::asio::async_read_until(mSocket, mResponse, "\r\n",
            boost::bind(&handle_read_status_line, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_read_status_line(const boost::system::error_code& err)
{
    if (!err)
    {
        std::istream response_stream(&mResponse);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/")
        {
            return;
        }
        if (status_code != 200)
        {
            return;
        }
    }
    else
    {
        delete this;
    }
}

【问题讨论】:

  • 你在任何时候都会调用 ioservice::run() 吗?只是问一下,因为这是一个常见的错误。
  • 在每个处理程序中放置一些调试消息,并让我们知道您的进展情况,尤其是主机解析到的端点或 IP 地址。但是,既然您说 Fiddler 没有显示任何内容,我的猜测是连接也失败了。此外,从等式中删除 Fiddler 并改用 Wireshark/tcpdump 进行测试。

标签: c++ boost https asio


【解决方案1】:

看来只是改变了这一行:

tcp::resolver::query query(host, "https");

有了这个:

tcp::resolver::query query(proxyUrl, proxyPort);

成功了。

问题在于不允许 HTTPS 通信的代理配置。

【讨论】:

猜你喜欢
  • 2011-03-06
  • 1970-01-01
  • 2012-05-15
  • 2021-12-11
  • 2010-11-21
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
相关资源
最近更新 更多