简体   繁体   中英

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?

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. First call can be served by server with utf-8, the other can be served by another server with another encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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