URLConnection没有得到字符集 [英] URLConnection does not get the charset

查看:126
本文介绍了URLConnection没有得到字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 URL.openConnection()从服务器下载内容。服务器说

I'm using URL.openConnection() to download something from a server. The server says

Content-Type: text/plain; charset=utf-8

但是 connection.getContentEncoding()返回 null 。怎么了?

推荐答案

这是记录的行为,因为 getContentEncoding()指定方法返回 Content-Encoding HTTP标头的内容,该标头未在您的示例中设置。您可以使用 getContentType()方法并自行解析生成的String,或者可能需要更多高级 HTTP客户端库,如 Apache

This is documented behaviour as the getContentEncoding() method is specified to return the contents of the Content-Encoding HTTP header, which is not set in your example. You could use the getContentType() method and parse the resulting String on your own, or possibly go for a more advanced HTTP client library like the one from Apache.

这篇关于URLConnection没有得到字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆