Get Content type of HttpURLConnection.getInputStream()

duplex143 :

I'm having trouble using HttpUrlConnection. I'm working on multiple servers. Some servers send response in gzip encoding and some don't. For gzip encoding, I'm using

inputStream = new GZIPInputStream(connection.getInputStream());
inputStreamReader = new InputStreamReader(inputStream);

And for normal encoding, I'm using

inputStreamReader = new InputStreamReader(connection.getInputStream());

Is it possible to know the encoding of getInputStream so that I know beforehand whether or not to use GZIPInputStream. Or is there a generic input stream reader for both compressed & uncompressed. Thanks.

Robby Cornelissen :

Get the content encoding from the HttpURLConnection using getContentEncoding().

It it's gzip-encoded, the result of that call should be gzip, and then you know what type of input stream you need to create.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23161&siteId=1