如何解码Java中的http POST数据? [英] How to decode http POST data in Java?

查看:996
本文介绍了如何解码Java中的http POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Netty,我必须接受并解析http POST请求。据我所知,Netty没有内置的POST支持,只有GET。 (这是一个处理原始网络操作的相当低级的库。使用servlet容器,它可以完成所有这些开箱即用的功能。)

I'm using Netty, and I've got to accept and parse http POST requests. As far as I can tell, Netty doesn't have built-in support for POSTs, only GETs. (It's a fairly low-level library that deals with primitive network operations. Using a servlet container, which does all this stuff out of the box, is not an option.)

如果我将POST请求的内容作为字节数组,那么将它解析为参数Map的最快且最无错误的方法是什么?

If I have the content of a POST request as an array of bytes, what's the fastest and most bug-free way to parse it into a Map of parameters?

I可以自己编写,但必须是JDK中内置的一些方法,这使得这更容易。我敢打赌,还有一些问题需要处理。

I could write this myself, but there must be some methods built into the JDK that make this easier. And I'll bet there are some gotchas and corner cases to deal with.

推荐答案

Netty有一个高级的POST请求解码器(HttpPostRequestDecoder) )它可以解码
Http属性,带有分块编码的FileUpload内容。

Netty has an advanced POST request decoder (HttpPostRequestDecoder) which can decode Http Attributes, FileUpload Content with chunked encoding.

这是一个简单的表单解码示例

Here is an simple form decoding example

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  HttpRequest request = (HttpRequest) e.getMessage();
  HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);

  InterfaceHttpData data = decoder.getBodyHttpData("fromField1");
  if (data.getHttpDataType() == HttpDataType.Attribute) {
     Attribute attribute = (Attribute) data;
     String value = attribute.getValue()
     System.out.println("fromField1 :" + value);
  }
}

这篇关于如何解码Java中的http POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆