我尝试使用Microsoft REST SDK发送和接收protobuf对象。如果您不熟悉protobuf,请想象它可以是任何其他原始数据。在客户端,向量为空。我认为我没有在服务器端正确创建流。
服务器的get方法如下所示:
.dont-break-out {
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
/* This is the dangerous one in WebKit, as it breaks things wherever */
word-break: break-all;
/* Instead use this non-standard one: */
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
像这样的客户端方法:
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;
};
答案 0 :(得分:0)
我回答我自己的问题。没有必要流;我可以通过这种方式使用二进制数据:
服务器:
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");
int size = user->ByteSize();
unsigned char *buffer = new unsigned char(size);
user->SerializeToArray(buffer, size);
std::string out;
user->SerializeToString(&out);
std::vector<unsigned char> data(buffer, buffer + size);
string_t outT = utility::conversions::to_base64(data);
message.reply(status_codes::OK, outT);
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;
geopp::gnUser *user = new geopp::gnUser();
auto rslt = response.extract_string();
auto rslt0 = utility::conversions::from_base64(rslt.get());
user->ParseFromArray(rslt0.data(), rslt0.size());
cout << "Result: name <" << user->name() << ">, id <" << user->id() << ">" << endl;
};
答案 1 :(得分:0)
服务器端:
std::vector<uint8_t> data;
//TODO: fill data
//convert the vector into a async istream
auto instream = Concurrency::streams::bytestream::open_istream(data);
message.reply(status_codes::OK, instream);
客户方:
wchar_t urlbuf[256];
//TODO: fill the url
client->request(web::http::methods::GET,urlbuf).then([](web::http::http_response& resp){
if(resp.status_code() != status_codes::OK){
DLogError("error: status != ok");
return;
}
Concurrency::streams::container_buffer<std::vector<uint8_t> >inBuffer;
resp.body().read_to_end(inBuffer).then([](size_t bytesRead){
//TODO: use it
}
}