【问题标题】:Using cpp-netlib 0.8 as a HTTP Proxy - Helpless使用 cpp-netlib 0.8 作为 HTTP 代理 - 无奈
【发布时间】:2011-01-13 09:31:01
【问题描述】:

我正在尝试在我的项目中使用 cpp-netlib。我只需要创建一个 HTTP 代理,稍后我可以在其中插入一些功能。现在,我只需要监听请求,将请求发送到新站点,并将新请求的响应转发给第一个请求。

这是我目前的代码:

std::string ip = source(request);
http::client client;
std::ostringstream url;
url << "www.example.com/image.jpg";
http::client::request clientRequest(url.str());
http::client::response clientResponse = client.get(clientRequest);
response = server::response::stock_reply(server::response::ok);
response.headers = clientResponse.headers(); //This is not possible - not correct type or something
response.content = clientResponse.body();

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::multimap<_Kty,_Ty>' (or there is no acceptable conversion) 中的结果

对我正在使用的测试图像的请求会在响应中产生 19.4kb 的数据。如果我通过上述代码执行相同的请求(不复制标头),我会得到大约 4kb 数据的答案,浏览器会尝试将其显示为文本(默认标头)。它看起来确实像一张图片,即使是文本。

有熟悉 cpp-netlib-0.8 的人吗? response.content = clientResponse.body(); 是正确的方法吗?如何添加正确的标题?

cpp-netlib 中的模板太奇怪了,我现在无法理解!

谢谢...

【问题讨论】:

    标签: c++ http networking


    【解决方案1】:

    而不是使用:

    response.content = clientResponse.body();
    

    正确的做法是:

    std::string body_content = body(clientResponse);
    

    另外,您使用response 作为变量,而您的变量实际上是clientResponse

    更多示例请阅读cpp-netlib v0.8 documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2020-02-20
      相关资源
      最近更新 更多