简体   繁体   English

HttpUrlConnection读取inputstream-不同的输出结果

[英]HttpUrlConnection read inputstream - different output result

Below method is designed to get source code of html page of given url, but it returns result in different charset in each call (in every call argument url is same), please, explain me why? 下面的方法旨在获取给定url的html页面的源代码,但是它在每个调用中返回不同字符集的结果(每个调用参数url都相同),请解释为什么?

private String getSourceCode(URL url) {
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel MacOS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    return IOUtils.toString(conn.getInputStream()); 
}

Actually there are several possible reasons. 实际上,有几种可能的原因。 For example behind the URL there can actually be several different servers with different default encoding of response. 例如,在URL后面实际上可以有几台不同的服务器,它们的响应默认编码不同。 First call can be served by server with utf-8, the other can be served by another server with another encoding. 第一次呼叫可以由服务器使用utf-8进行服务,另一个可以由另一台服务器使用另一种编码进行服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM