restfull服务器端如何接收文件,cpprest 服务器端向客户端reply后,客户端收不到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();

// Open stream to output file.

pplx::task 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值