2011-09-06 70 views
2
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); 

不產生有效請求。什麼是發送發佈請求的正確方式,最好是通過手動構建參數數據而不是使用DataFactory。另外,爲什麼HttpDataFactory不包含在任何發行版中?用Netty發送POST params,爲什麼DefaultHttpDataFactory不在發行版中?

回答

2

你寫的一切正確,只需加上httpReq.setHeader(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded"); 你的例子就可以工作。對於更復雜的代碼,您需要添加網址編碼。

0
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toASCIIString()); 
request.headers().set(HttpHeaders.Names.HOST, ip); 
request.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded"); 
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
nvps.add(new BasicNameValuePair(param.getKey(), param.getValue())); 
HttpEntity httpEntity = new UrlEncodedFormEntity(nvps); 
ByteBuf byteBuf = 
Unpooled.copiedBuffer(EntityUtils.toByteArray(httpEntity)); 
request.content().writeBytes(byteBuf); 
request.headers().set(HttpHeaders.Names.CONTENT_LENGTH,request.content().readableBytes()); 
fu.channel().writeAndFlush(request) 
相關問題