[英]obtaining response charset of response to get or post request
I am working to extract response charset in a java web app, where I am using Apache HTTP Client. 我正在java web应用程序中提取响应字符集,我正在使用Apache HTTP Client。
For example, one possible value obtained from "Content-Type" header is 例如,从“Content-Type”标题获得的一个可能值是
text/html; charset=UTF-8
Then my code will extract all text after the "=" sign... 然后我的代码将在“=”符号后提取所有文本...
So the charset as extracted will be 所以提取的字符集将是
UTF-8
I just wanted to know, is the above method for obtaining response charset correct? 我只是想知道,上面获取响应字符集的方法是否正确? Or is there some scenario where the above code will not work? 或者是否存在上述代码不起作用的情况? Is there something I am missing here? 这里有什么我想念的吗?
Doesn't httpclient (or http core) already provide that functionality? httpclient(或http核心)是否已经提供了该功能? Something like this: 像这样的东西:
HttpResponse response = ...
String charset = EntityUtils.getContentCharSet(response.getEntity());
The method provided by forty-two can work. 由四十二提供的方法可以工作。 But the method is deprecated, I find out that this website has a good example of method to find the charset. 但该方法已弃用,我发现该网站有一个很好的查找字符集的方法示例。
HttpEntity entity = response.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
System.out.println("Charset = " + charset.toString());
Well, that approach will fail when 那么,这种方法会失败
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.