cpprest客户端没有收到任何回复

时间:2015-06-02 07:32:57

标签: c++ rest http-error

当我构建自己的cpprestsdk服务器和客户端时,我发现当我的服务器收到请求并回复它时,我的客户端对它没有反应,它永远不会进入我处理http_response的断点,这里是我的代码;

我被困了这么多天,有人会帮我解决这个问题,非常感谢 (客户端发送请求,服务器接收并回复,客户端无法接收http_response)

服务器(我刚从互联网上的某处获得):

#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;
}

这是我的客户

   #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;
}

0 个答案:

没有答案