简体   繁体   English

在Java中,如何仅对协议缓冲区的内容进行HttpPost?

[英]In Java, how can I HttpPost only the contents of a protocol buffer?

I've tried using a ByteArrayEntity, as below, but this sends extra bytes that aren't in the protocol buffer itself. 我已经尝试过使用ByteArrayEntity,如下所示,但这会发送协议缓冲区本身中没有的额外字节。 Is there an Entity which just POSTs the contents? 是否有一个仅发布内容的实体?

        byte [] info;

        ... [info initialized] ...

        HttpPost httppost = new HttpPost("https://myurl");
        ByteArrayEntity reqEntity = new ByteArrayEntity(info);

        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true);
        httppost.setEntity(reqEntity);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

The answer is disappointingly banal: the extra bytes were added by the call to setChunked(true). 令人失望的答案是平庸的:额外的字节是通过调用setChunked(true)添加的。 Removing the call results in only the bytes in "info" getting sent. 删除呼叫只会导致发送“ info”中的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM