简体   繁体   English

为什么在使用 HttpClient 时分块流意外结束?

[英]Why am I getting chunked stream ended unexpectedly when I use HttpClient?

The problem only seems to occur with "large" files I'm trying to post.该问题似乎只出现在我尝试发布的“大”文件中。

My code looks like this:我的代码如下所示:

PostMethod method = new PostMethod(url);

File input = new File(filePathname);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");

method.setRequestEntity(entity);
method.setRequestHeader("Content-Disposition", "attachment; filename=xyzzy")

HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("userid", "pw");

client.getState().setCredentials(new AuthScope("hostname", port, AuthScope.ANY_REALM), defaultcreds);

 try {
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        throw new Exception("Method failed: " + method.getStatusLine());
    }

    // Read the response body.
    byte[] responseBody = method.getResponseBody();
    return new String(responseBody);
 }
 catch (HttpException e) {
    System.err.println("Fatal protocol violation: " + e.getMessage());
    throw e;

 }
 catch (IOException e) {
    System.err.println("Fatal transport error: " + e.getMessage());
    throw e;

 }
 finally {
     // Release the connection.
     method.releaseConnection();
 }

The exception text looks like this:异常文本如下所示:

Fatal transport error: chunked stream ended unexpectedly
Exception in thread "main" java.io.IOException: chunked stream ended unexpectedly
    at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252)
    at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221)
    at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176)
    at java.io.FilterInputStream.read(FilterInputStream.java:127)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108)
    at java.io.FilterInputStream.read(FilterInputStream.java:101)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127)
    at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)

I get a similar exception whether I use getResponseBody() or getResponseBodyAsStream().无论我使用 getResponseBody() 还是 getResponseBodyAsStream(),我都会遇到类似的异常。

I shouldn't be getting much data back, but I am posting over 200mb of data.我不应该获得太多数据,但我发布了超过 200MB 的数据。

I am able to make this problem go away by altering the length of the file name value specified in the PostMethod's requestHeader.我可以通过更改 PostMethod 的 requestHeader 中指定的文件名值的长度来解决这个问题。 I had been including an encoded version of the full file pathname in the request header.我一直在请求标头中包含完整文件路径名的编码版本。 Through trial and error I found that success or failure of the file I was "posting" seemed to depend on the folder it was in. A long folder file pathname wasn't working while a short one, albeit with the same file, was.通过反复试验,我发现我“发布”的文件的成功或失败似乎取决于它所在的文件夹。长文件夹文件路径名不起作用,而较短的文件路径名不起作用,尽管具有相同的文件。 So I eliminated the path name from the request header and only started including the file name and I'm no longer seeing the problem.所以我从请求头中删除了路径名,只开始包含文件名,我不再看到问题。

May be old , Can save some time..... I got this error where Server is in Python and Clinet is Java.可能是旧的,可​​以节省一些时间..... 我在 Server 是 Python 而 Clinet 是 Java 时遇到了这个错误。

1st -第一 -

Error from Java Client "Error while sending data over http java.io.IOException: CRLF expected at end of chunk: 79/82 java.io.IOException: CRLF expected at end of chunk: 79/82"来自 Java 客户端的错误“通过 http java.io.IOException 发送数据时出错:块末尾预期 CRLF:79/82 java.io.IOException:块末尾预期 CRLF:79/82”

2nd -第二 -

Error from Java Clinet "Error while sending data over http java.io.IOException: chunked stream ended unexpectedly java.io.IOException: chunked stream ended unexpectedly"来自 Java Clinet 的错误“通过 http java.io.IOException 发送数据时出错:分块流意外结束 java.io.IOException:分块流意外结束”

Both the errors got resolved by changing the ok response with chunked stream size通过使用分块流大小更改 ok 响应来解决这两个错误

One with issues -一个有问题的——

HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\nDE\r\n

Resolved with -解决了 -

HTTP/1.1 200 OK\r\nContent-Length: 20000\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\n229\r\n

Note = nDE is replaced with n229注意 = nDE 替换为 n229

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MalformedChunkCodingException:分块的流意外结束 - MalformedChunkCodingException: Chunked stream ended unexpectedly 泽西服务器JSON响应引发MalformedChunkCodingException:分块的流意外结束 - Jersey server JSON response throwing MalformedChunkCodingException: Chunked stream ended unexpectedly 为什么我用Apache Commons FileUpload获得“FileUploadException:Stream意外结束”? - Why did I get “FileUploadException: Stream ended unexpectedly” with Apache Commons FileUpload? 为什么在android中使用webview时出现错误? - Why I am getting an error when I use webview in android? 直播意外结束 - Stream ended unexpectedly 尝试在javafx中使用css时为什么会出现InvocationTargetException? - Why am I getting InvocationTargetException when trying to use css with javafx? 为什么在 Spring Cloud Stream 反应式消费者中遇到异常时会收到 onComplete 信号? - Why am I getting onComplete signal when an exception is encountered in Spring Cloud Stream reactive consumer? 为什么我在解密时收到“BadPaddingException”? - Why am I getting 'BadPaddingException' when decrypting? 为什么使用try catch时会出现错误 - Why i am getting Error if i use try catch 使用意图后为什么会出现此错误? - Why am I getting this error after I use intent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM