Dale
Dale

Reputation: 3547

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().

I shouldn't be getting much data back, but I am posting over 200mb of data.

Upvotes: 2

Views: 12042

Answers (2)

Malasani
Malasani

Reputation: 25

May be old , Can save some time..... I got this error where Server is in Python and Clinet is 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"

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"

Both the errors got resolved by changing the ok response with chunked stream size

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

Upvotes: 1

Dale
Dale

Reputation: 3547

I am able to make this problem go away by altering the length of the file name value specified in the PostMethod's 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.

Upvotes: 1

Related Questions