[DEBUG] DefaultClientConnection - Sending request: GET / HTTP/1.1
[DEBUG] headers - >> GET / HTTP/1.1
[DEBUG] headers - >> Host: kokos.pl
[DEBUG] headers - >> Connection: Keep-Alive
[DEBUG] headers - >> User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
[DEBUG] DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
[DEBUG] headers - << HTTP/1.1 200 OK
[DEBUG] headers - << Server: nginx
[DEBUG] headers - << Date: Thu, 01 Aug 2013 12:04:12 GMT
[DEBUG] headers - << Content-Type: text/html
[DEBUG] headers - << Connection: keep-alive
...
この URI に対してサーバーから返される応答メッセージでは、コンテンツの文字セットが明示的に指定されていません。このような場合、HttpClient は HTTP コンテンツにデフォルトの文字セット エンコーディングを使用することを余儀なくされISO-8859-1
ますUTF-8
。
残念ながら、流暢な API で使用されるデフォルトのコンテンツ文字セットをオーバーライドする唯一の方法は、カスタム応答ハンドラーを使用することです。
ResponseHandler<String> myHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws IOException {
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
}
};
String html = Request.Get("https://kokos.pl/").execute().handleResponse(myHandler);
System.out.println(html);