C ++ boost :: asio https通过代理

时间:2017-08-10 15:13:51

标签: c++ boost https asio

所以我使用boost :: asio来连接ssl:

,这个代码很好用

打开小提琴手,但我根本看不到这些联系。 从我所读到的内容中我收集到的,我必须通过代理传递请求。 所以我尝试了所有可以找到的例子和答案,但我无法使其发挥作用。

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

我错过了什么?

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;
    }
}

1 个答案:

答案 0 :(得分:0)

似乎只是改变了这一行:

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

有了这个:

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

诀窍。

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