javaapache-httpclient-4.xchunked-encodingchunkedhttp-chunked

Using HttpClient 4.1 to decode chunked data


I am using HttpClient to send a request a server which is supposed to return xml data. This data is returned as chunked data. I am then trying to write the received xml data to a file. The code I use is shown below:

    HttpEntity entity = response.getEntity();
    InputStream instream = entity.getContent();

        try {
            // do something useful
            InputStreamReader isr = new InputStreamReader(instream);
            FileWriter pw;

            pw = new FileWriter(filename, append);

            OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream(filename, append), "UTF-8");
            BufferedReader rd = new BufferedReader(isr);

            String line = "";
            while ((line = rd.readLine()) != null) {
                // pw.write(line);
                outWriter.write(line);
            }
            isr.close();
            pw.close();
        } finally {
            instream.close();
        }

This results in data that looks as follows to be printed to the file: resulting file

This code works for non chunked data. How do I properly handle chunked data responses using HttpClient. Any help is greatly appreciated.


Solution

  • I don't think that your problem is the chunking of data.

    XML data is plain text data - chunking it means that it is split into several parts that are transfered after another. Therefore each chunk should contain visible plain text xml data which is obviously not the case as shown in the data picture.

    May be the content is encoded compressed via gzip or it is not plain text XML but binary encoded XML (e.g. like WBXML).

    What concrete type you have you can see from the sent server response headers, especially the used mime type it contains.