简体   繁体   English

Netty:将带有二进制数据的表单字段添加到HTTP POST请求

[英]Netty: adding a form field with binary data to a HTTP POST request

In Netty there is a HttpPostRequestEncoder class which has an addBodyAttribute(String name, String value) method. 在Netty中,有一个HttpPostRequestEncoder类,该类具有addBodyAttribute(String name, String value)方法。 I need to POST a value with binary data, but I can not find a method which takes byte[] value instead of String value . 我需要发布带有二进制数据的值,但是我找不到采用byte[] value而不是String value Is it easily achievable by any other means? 是否可以通过其他任何方式轻松实现?

One may try to use the following (supposely wrong) combination if in standard POST (application/x-www-form-urlencoded): 1) Creating his/her own data = InterfaceHttpData (based for instance on MemoryAttribute ) and filling it with a ChannelBuffer / ByteBuffer (according to Netty version) build from the given byte[] ( setContent() ) 如果在标准POST(应用程序/ x-www-form-urlencoded)中,可能会尝试使用以下(可能是错误的)组合:1)创建他/她自己的data = InterfaceHttpData (例如基于MemoryAttribute ),并用一个从给定的byte []( setContent() )构建ChannelBuffer / ByteBuffer (根据Netty版本)

2) Use addBodyHttpData(InterfaceHttpData data) 2)使用addBodyHttpData(InterfaceHttpData data)

But the issue is, as far as I know, RFC does not allow "byte" for application/x-www-form-urlencoded ; 但是据我所知,问题是RFC不允许application / x-www-form-urlencoded使用“字节”; therefore the value will have to be encoded using default charset, which is likely to not produce the correct answear. 因此,必须使用默认字符集对值进行编码,这可能不会产生正确的响应。

Edit: To leverage this, one can create the encoder accordingly to use multipart: 编辑:要利用这一点,可以相应地创建编码器以使用多部分:

new HttpPostRequestEncoder(factory, req, true);

It should work... 应该可以...

A) So possibly you will have to first encoded your "byte" in a "text" way (7 bit way as BASE64 encoding), then create your attribute using the default constructor addBodyAttribute(String name, String value) and then decoding on server side the value back to binary using BASE64 decoder. A)因此,您可能必须首先以“文本”方式(“ BASE64编码为7位”)对“字节”进行编码,然后使用默认构造函数addBodyAttribute(String name, String value)创建属性,然后在服务器上解码使用BASE64解码器将值返回二进制值。

=> But this needs the server to perform special decoding action (BASE64) =>但这需要服务器执行特殊的解码操作(BASE64)

B) Another option would be to create a multipart POST request and then create a "fictive" file upload, using for instance MemoryFileUpload , using the setContent() from your own ChannelBuffer / ByteBuffer (according to Netty version) build from the given byte[] ; B)另一种选择是创建一个多部分POST请求,然后使用MemoryFileUpload创建一个虚构的文件上传,例如使用从给定字节构建的ChannelBuffer / ByteBuffer (根据Netty版本)的setContent() 。 ]; therefore the server side will have to handle the bynary content as usual (no decoding needed). 因此,服务器端将不得不照常处理常规内容(无需解码)。

After some digging into Netty sources I've found somewhat looking as a solution: 在深入研究Netty来源之后,我发现了一些解决方案:

byte[] buf = new byte[200];
...
DefaultFullHttpRequest req = new DefaultFullHttpRequest(httpVersion, HttpMethod.POST, uri);
DefaultHttpDataFactory factory = new DefaultHttpDataFactory();
HttpPostRequestEncoder enc = new HttpPostRequestEncoder(factory, req, false, CharsetUtil.UTF_8, HttpPostRequestEncoder.EncoderMode.RFC1738);
Attribute attr = factory.createAttribute(req, "someBinaryVar");
attr.setContent(io.netty.buffer.Unpooled.wrappedBuffer(buf));
enc.addBodyHttpData(attr);
...
enc.finalizeRequest();

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

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