如何为 boost::asio 添加代理支持?

2021-12-31 00:00:00 proxy c++ boost-asio

在我的桌面应用程序中,我使用 boost::asio 添加了对各种互联网资源的访问.我所做的就是发送 http 请求(即映射图块服务器)并读取结果.我的代码基于 asio sync_client 示例.

In my desktop application I added access to various internet resources using boost::asio. All i do is sending http requests (i.e to map tile servers) and read the results. My code is based on the asio sync_client sample.

现在我收到客户的报告,他们在公司中运行代理而无法使用这些功能.在网络浏览器中,他们可以输入他们的代理地址,一切都很好.我们的应用程序无法下载数据.

Now i get reports from customers who are unable to use these functions as they are running a proxy in their company. In a web browser they can enter the address of their proxy and everything is fine. Our application is unable to download data.

如何为我的应用程序添加此类支持?

How can i add such support to my application?

推荐答案

我自己找到了答案.很简单:

I found the answer myself. It's quite simple:

http://www.jmarshall.com/easy/http/#proxies对 http 代理的工作原理进行了相当简洁明了的描述.

http://www.jmarshall.com/easy/http/#proxies gives quite a brief and clear description how http proxies work.

我所要做的就是将以下代码添加到 asio sync_client 示例示例中:

All i had to do is add the following code to the asio sync_client sample sample :

std::string myProxyServer = ...;
int         myProxyPort   = ...;

void doDownLoad(const std::string &in_server, const std::string &in_path, std::ostream &outstream)
{
    std::string server      = in_server;
    std::string path        = in_path;
    char serice_port[255];
    strcpy(serice_port, "http");

    if(! myProxyServer.empty())
    {
        path   = "http://" + in_server + in_path;
        server = myProxyServer;
        if(myProxyPort    != 0)
            sprintf(serice_port, "%d", myProxyPort);
    }
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(server, serice_port);

...

相关文章