how to add proxy support to boost::asio?

后端 未结 2 876
陌清茗
陌清茗 2020-12-16 22:12

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. M

相关标签:
2条回答
  • 2020-12-16 22:29

    It seems that sample is merely a show-off of what Boost ASIO can be used for but is likely not intended to be used as-is. You should probably use a complete library that handles not only HTTP proxies, but also HTTP redirects, compression, and so on.

    HTTP is a complex thing: without doing so, chances are high that you will get news from another client soon with another problem.

    I found cppnetlib which looks promising and is based on Boost ASIO not sure it handles proxies though.

    There is also libcurl but I don't know if it can easily be integrated with Boost ASIO.

    0 讨论(0)
  • 2020-12-16 22:35

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

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

    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);
    
    ...
    
    0 讨论(0)
提交回复
热议问题