@Test
public void testGetHtmlCharset() throws Exception {

    HttpServer server = httpServer(13423);
    server.get(by(uri("/header"))).response(header("Content-Type", "text/html; charset=gbk"));
    server.get(by(uri("/meta4"))).response(with(text("<html>\n" +
            "  <head>\n" +
            "    <meta charset='gbk'/>\n" +
            "  </head>\n" +
            "  <body></body>\n" +
            "</html>")),header("Content-Type","text/html; charset=gbk"));
    server.get(by(uri("/meta5"))).response(with(text("<html>\n" +
            "  <head>\n" +
            "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\" />\n" +
            "  </head>\n" +
            "  <body></body>\n" +
            "</html>")),header("Content-Type","text/html"));
    Runner.running(server, new Runnable() {
        @Override
        public void run() {
            String charset = getCharsetByUrl("http://127.0.0.1:13423/header");
            assertEquals(charset, "gbk");
            charset = getCharsetByUrl("http://127.0.0.1:13423/meta4");
            assertEquals(charset, "gbk");
            charset = getCharsetByUrl("http://127.0.0.1:13423/meta5");
            assertEquals(charset, "gbk");
        }
        private String getCharsetByUrl(String url) {
            HttpClientDownloader downloader = new HttpClientDownloader();
            Site site = Site.me();
            CloseableHttpClient httpClient = new HttpClientGenerator().getClient(site);
            // encoding in http header Content-Type
            Request requestGBK = new Request(url);
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(new HttpUriRequestConverter().convert(requestGBK, site, null).getHttpUriRequest());
            } catch (IOException e) {
                e.printStackTrace();
            }
            String charset = null;
            try {
                byte[] contentBytes = IOUtils.toByteArray(httpResponse.getEntity().getContent());
                charset = CharsetUtils.detectCharset(httpResponse.getEntity().getContentType().getValue(), contentBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return charset;
        }
    });
}