ChunkedInputStream bad chunk size from IIS server - Any ideas?

R

Ray Thomas

I'm trying to use the apache commons httpclient package to send a
request to an application running on an IIS 5 server. The response
returned from the server contains the "Transfer-encoding: chunked"
header but does not seem to be sending the proper chunk size
information in the response body. The ChunkedInputStream chokes on
the response data and throws an exception that appears to result from
an attempt to parse a chunk size out of part of the actual page data:

------------------------------------------------------------------------
java.io.IOException: Bad chunk size: <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 3.2 Final//EN">
at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:320)
at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:237)
at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:192)
at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:212)
------------------------------------------------------------------------



The code I'm using (fairly simple stuff but in case I'm missing
something...):

String xferEncHeader = getHeader("Transfer-Encoding");
boolean usesChunkedTransferEncoding = "chunked".equals(xferEncHeader);
if (usesChunkedTransferEncoding) {
InputStream chunkedResponseData = new
ChunkedInputStream(httpMethod.getResponseBodyAsStream(),
httpMethod);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] xferbuf = new byte[1024];
int xferbytes = -1;
while ((xferbytes = chunkedResponseData.read(xferbuf)) != -1) {
buffer.write(xferbuf, 0, xferbytes);
}
buffer.flush();
String localResponseBodyCache = buffer.toByteArray();
}

Any ideas what's wrong? Is it a bug in
commons.httpclient.ChunkedInputStream? Does anyone know a way to work
around it?
 
R

Ray Thomas

I'll answer my own question since (as often is the case with me) I
figured it out a few minutes after I became frustrated enough to post
a question. Anyway, the ChunkedInputStream, although it is a public
class in the httpclient library, is apparently not intended to be used
directly in the client code. Maybe it should be left as package
instead (nudge, nudge). I thought I'd trace through what
ChunkedInputStream was doing so I cranked up the app in a debugger and
put a breakpoint in the ChunkedInputStream.read() method. Turns out
that ChunkedInputStream is already used internally by the HttpMethod
getResponseBody() methods so if the client application tries to wrap
the output data in a ChunkedInputStream again, the chunk protocol
stuff has already been stripped out. Doh!

Hope this helps the next person that tries to handle this the hard way
like I was doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top