我尝试使用 Microsoft REST SDK 发送和接收 protobuf 对象。如果您不熟悉 protobuf,可以想象它可以是任何其他原始数据。在客户端,向量是空的。我认为我没有在服务器端正确创建流。
服务端的get方法如下:
void MyServer::handle_get(http_request message)
{
// a class created with protobuf
ns::user *user = new ns::user();
ns::user->set_id(-1);
ns::user->set_name("Dark");
ns::user->set_email("dark@foo.com");
// this schould be fine
int size = user->ByteSize();
void *buffer = malloc(size);
user->SerializeToArray(buffer, size);
// how to do?
concurrency::streams::container_buffer<std::vector<char>> inBuffer;
concurrency::streams::istream bodyStream = message.body();
inBuffer.putn((char*)buffer, size);
Concurrency::task<size_t> read = bodyStream.read_to_end(inBuffer);
message.reply(status_codes::OK, bodyStream, size, L"application/octet-stream");
delete user;
};
客户端方法如下:
utility::ostringstream_t buf;
buf << "Dark" << "/" << "Foo" << "?" << "id=" << "1";
http_response response = CheckResponse("server/user", server.request(methods::GET, buf.str()).get());
if (response.status_code() == status_codes::OK)
{
cout << "STATUS OK" << endl;
Concurrency::streams::istream iStream = response.body();
auto rslt0 = response.extract_vector();
auto rslt = rslt0.get();
cout << "Result has size: <" << rslt.size() << ">" << endl;
for (unsigned int i = 0; i < rslt.size(); i++)
{
cout << "Data" << i << ": <" << rslt.at(i) << ">" << endl;
}