8

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?

4

2 回答 2

7

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

http://www.jmarshall.com/easy/http/#proxies 对 http 代理的工作方式进行了非常简短和清晰的描述。

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

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

...
于 2012-07-18T08:51:01.103 回答
3

该示例似乎只是展示 Boost ASIO 的用途,但可能不打算按原样使用。您可能应该使用一个完整的库,该库不仅可以处理 HTTP 代理,还可以处理 HTTP 重定向、压缩等。

HTTP 是一件复杂的事情:如果不这样做,您很可能很快就会从另一个客户端获得另一个问题的消息。

我发现cppnetlib看起来很有前途,并且基于 Boost ASIO,但不确定它是否可以处理代理。

还有libcurl但我不知道它是否可以轻松地与 Boost ASIO 集成。

于 2012-07-17T15:25:42.260 回答