本文介绍了使用Netty发送POST参数,为什么DefaultHttpDataFactory不在版本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
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请求的正确方法是什么,最好是手动构造参数数据而不是DataFactory。另外,为什么HttpDataFactory不包含在任何版本中?

Does not yield a valid request. What is the correct way to send a post request, preferably by constructing the parameters data manually as opposed to with the DataFactory. Also, why is HttpDataFactory not included in any of the releases?

推荐答案

你写的一切都是正确的,只需添加 httpReq.setHeader(HttpHeaders.Names.CONTENT_TYPE,application / x-www-form-urlencoded);
,您的示例将起作用。对于更复杂的代码,您需要添加url编码。

You wrote everything correct, just add httpReq.setHeader(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");and your example will work. For more complex code you need to add url encoding.

这篇关于使用Netty发送POST参数,为什么DefaultHttpDataFactory不在版本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:58