Java HttpRequest返回空实体,无法在服务器上提取正文

Java HttpRequest返回空实体,无法在服务器上提取正文,java,http,apache-httpcomponents,Java,Http,Apache Httpcomponents,我正在尝试使用解析HTTP请求,并希望获取请求的主体。看起来默认的DefaultHttpRequestParser没有从输入流解析主体/实体。有这样的课程吗 不幸的是,我不能使用整个堆栈,需要直接从这个输入流中提取请求 我的解析代码如下。从其他一些答案来看,请求主体似乎应该作为一个实体提供。但是,每次我尝试获取实体时,它都是空的 调试时,我看到缓冲区已读取但未使用主体,并且DefaultHttpRequestParser似乎只是读取头。我是否应该使用解析来解析整个输入 InputStre

我正在尝试使用解析HTTP请求,并希望获取请求的主体。看起来默认的
DefaultHttpRequestParser
没有从输入流解析主体/实体。有这样的课程吗

不幸的是,我不能使用整个堆栈,需要直接从这个输入流中提取请求

我的解析代码如下。从其他一些答案来看,请求主体似乎应该作为一个实体提供。但是,每次我尝试获取实体时,它都是空的

调试时,我看到缓冲区已读取但未使用主体,并且
DefaultHttpRequestParser
似乎只是读取头。我是否应该使用解析来解析整个输入

    InputStream is = socket.getInputStream();
    HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
    SessionInputBufferImpl buf = new SessionInputBufferImpl(metrics, 2048);
    buf.bind(is);

    DefaultHttpRequestParser reqParser = new DefaultHttpRequestParser(buf);
    HttpRequest req = reqParser.parse();
    if (req instanceof HttpEntityEnclosingRequest) {
        entity = ((HttpEntityEnclosingRequest)query).getEntity();
        //... entity is always null
如果我读取了输入流,我将得到:

POST / HTTP/1.1
User-Agent: curl/Q.XX.0 (linux-gnu) libcurl/Q.XX.0 OpenSSL/X.Y.Z zlib/A.B.C.D libidn/E.FF librtmp/G.H
Host: localhost:8088
Accept: */*
Content-Length: 333
Content-Type: multipart/form-data; boundary=----------------------------39203c7982df

------------------------------39203c7982df
Content-Disposition: form-data; name="fileupload"; filename="grun.sh"
Content-Type: application/octet-stream

#!/bin/bash -x
java -classpath lib/antlr-4.4-complete.jar:build/classes org.antlr.v4.runtime.misc.TestRig Typegroup "AHI" -tree

------------------------------39203c7982df--
[Update]Oleg有一个很好的答案,但我可以将正文与请求关联起来吗 我现在需要传递两个东西,身体和溪流?我会调查的

我做了以下工作,但在最新版本中已被弃用

        ...
        HttpEntityEnclosingRequest ereq = (HttpEntityEnclosingRequest) req;
        @SuppressWarnings("deprecation")
        EntityDeserializer ed = 
           new EntityDeserializer(new LaxContentLengthStrategy());
        @SuppressWarnings("deprecation")//ack!
        HttpEntity ent = ed.deserialize(buf, req);
        ereq.setEntity(ent);
        return ereq;
将Oleg的解决方案与上述方法相结合,我最终得出:

            HttpEntityEnclosingRequest ereq = (HttpEntityEnclosingRequest) req;

            ContentLengthStrategy contentLengthStrategy =
                        StrictContentLengthStrategy.INSTANCE;
            long len = contentLengthStrategy.determineLength(req);
            InputStream contentStream = null;
            if (len == ContentLengthStrategy.CHUNKED) {
                contentStream = new ChunkedInputStream(buf);
            } else if (len == ContentLengthStrategy.IDENTITY) {
                contentStream = new IdentityInputStream(buf);
            } else {
                contentStream = new ContentLengthInputStream(buf, len);
            }
            BasicHttpEntity ent = new BasicHttpEntity();
            ent.setContent(contentStream);
            ereq.setEntity(ent);
            return ereq;
HttpCore中的消息解析器仅解析消息头。但是,可以从会话输入缓冲区继续读取,并读取消息正文内容,直到消息结束(取决于使用的描绘器)

InputStream is = socket.getInputStream();
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
SessionInputBufferImpl buf = new SessionInputBufferImpl(metrics, 2048);
buf.bind(is);

DefaultHttpRequestParser reqParser = new DefaultHttpRequestParser(buf);
HttpRequest req = reqParser.parse();
InputStream contentStream = null;
if (req instanceof HttpEntityEnclosingRequest) {
    ContentLengthStrategy contentLengthStrategy = StrictContentLengthStrategy.INSTANCE;
    long len = contentLengthStrategy.determineLength(req);
    if (len == ContentLengthStrategy.CHUNKED) {
        contentStream = new ChunkedInputStream(buf);
    } else if (len == ContentLengthStrategy.IDENTITY) {
        contentStream = new IdentityInputStream(buf);
    } else {
        contentStream = new ContentLengthInputStream(buf, len);
    }
}
// Do something useful with the content stream (if non null)