如何在Netty中发送带有POST参数的请求? [英] How to send a request with POST parameters in Netty?
问题描述
我正在尝试在Netty中发送带有POST参数的请求.
I'm trying to send a request with POST parameters in Netty.
我搜索了Netty API,Google和此处(堆栈溢出)
I searched Netty API, Google, and here (Stack Overflow)
但是没有找到任何好的方法. (这可能是我搜索技巧糟糕的错误:'(如果是,我表示歉意)
but didn't find any good way to do it. (It could be my fault of terrible searching skill :'( If so, I apologize)
有没有可以轻松实现的API?
Is there any API to do it easily?
还是我必须对所有参数进行编码并由我自己在内容中进行设置?
Or do I have to do it by encoding all parameters and setting it in the content by myself?
请让我知道执行此操作的任何好方法.
Please let me know any good way to do it.
推荐答案
下面是一个如何上传文件的示例:
Here's an example of how you would do a file upload:
https ://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/http/upload
如果您不想上传文件,只需忽略MIME multipart位.
If you don't want to upload a file, just ignore the MIME multipart bit.
尝试类似的东西:
HttpRequest httpReq=new DefaultHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.POST,uri);
httpReq.setHeader(HttpHeaders.Names.HOST,host);
httpReq.setHeader(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.KEEP_ALIVE);
httpReq.setHeader(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
httpReq.setHeader(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
String params="a=b&c=d";
ChannelBuffer cb=ChannelBuffers.copiedBuffer(params,Charset.defaultCharset());
httpReq.setHeader(HttpHeaders.Names.CONTENT_LENGTH,cb.readableBytes());
httpReq.setContent(cb);
请参见发送POST Netty的参数,为什么发行版中不没有DefaultHttpDataFactory?
这篇关于如何在Netty中发送带有POST参数的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!