西瓜巨人 2015-06-02 07:34 采纳率: 0%
浏览 2929

cpprest 客户端收不到http_response

客户端向服务端request请求后,服务器端能接收到http_request,然后reply回复,客户端就无法收到这个http_response,帮忙看看,实在找不出来原因了

客户端代码

 #include "cpprest/http_client.h"
#include "cpprest/filestream.h"

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[])
{
    auto fileStream = std::make_shared<ostream>();
    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;
        // Create http_client to send the request.
        http_client client(U("http://www.bing.com/"));
        http_client localclient(U("http://localhost:8888"));
        return localclient.request(methods::GET);
    })
        .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());
        system("pause");
        return response.body().read_to_end(fileStream->streambuf());

    })
        .then([=](size_t)
    {
        return fileStream->close();
    });
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
        system("pause");
    }

    return 0;
}

服务器代码

 #include "cpprest/json.h"
#include "cpprest/http_listener.h"
#include "cpprest/uri.h"
#include "cpprest/asyncrt_utils.h"
#include "cpprest/http_client.h"
using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;

void handle_get(http_request message)
{
    message.reply(status_codes::OK, U("Hello, World!"));
};

void handle_post(http_request message)
{
    message.reply(status_codes::NotFound);
};

void handle_put(http_request message)
{
    message.reply(status_codes::NotFound);
};

void handle_delete(http_request message)
{
    message.reply(status_codes::NotFound);
};

#define TRACE(msg)            std::wcout << msg
#define TRACE_ACTION(a, k, v) std::wcout << a << L" (" << k << L", " << v << L")\n"

int main(int argc, char ** argv)
{
    uri_builder uri(U("http://localhost:8888"));
    http_listener listener(uri.to_uri());

    listener.support(methods::GET, handle_get);
    listener.support(methods::POST, handle_post);
    listener.support(methods::PUT, handle_put);
    listener.support(methods::DEL, handle_delete);

    try
    {
        listener
            .open()
            .then([&listener](){TRACE(L"\nstarting to listen\n"); })
            .wait();

        while (true);
    }
    catch (std::exception const & e)
    {
        std::wcout << e.what() << std::endl;
    }
    catch (...)
    {
        std::wcout << "Unknown exception" << std::endl;
    }

    return 0;
}
  • 写回答

3条回答 默认 最新

  • oyljerry 2015-06-02 08:10
    关注

    客户端是否收到响应包,可以抓包分析一下客户端,服务端

    评论

报告相同问题?

悬赏问题

  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 C++行情软件的tick数据如何高效的合成K线
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
  • ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战
  • ¥15 svpwm波形异常求解答
  • ¥15 STM32——硬件IIC从机通信代码实现