0

我尝试使用 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;
            }
4

2 回答 2

2

服务器端:

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
  }
}
于 2016-05-17T10:12:34.647 回答
1

我回答我自己的问题。不需要流;我可以通过这种方式使用二进制数据:

服务器:

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;
};
于 2016-02-18T13:38:44.260 回答