I'm using URL.openConnection()
to download something from a server. The server says
Content-Type: text/plain; charset=utf-8
But connection.getContentEncoding()
returns null
. What up?
I'm using URL.openConnection()
to download something from a server. The server says
Content-Type: text/plain; charset=utf-8
But connection.getContentEncoding()
returns null
. What up?
The value returned from
URLConnection.getContentEncoding()
returns the value from headerContent-Encoding
Code from
URLConnection.getContentEncoding()
Instead, rather do a
connection.getContentType()
to retrieve the Content-Type and retrieve the charset from the Content-Type. I've included a sample code on how to do this....This is documented behaviour as the
getContentEncoding()
method is specified to return the contents of theContent-Encoding
HTTP header, which is not set in your example. You could use thegetContentType()
method and parse the resulting String on your own, or possibly go for a more advanced HTTP client library like the one from Apache.Just as an addition to the answer from @Buhake Sindi. If you are using Guava, instead of the manual parsing you can do: