您必须设置正确的编码。您可以在 HTTP 标头中找到编码:
Content-Type: text/html; charset=ISO-8859-1
这可能会在 (X)HTML 文档中被覆盖,请参阅HTML Character encodings
我可以想象您必须考虑许多不同的附加问题才能正确解析网页。但是有不同的可用于 Java 的 HTTP 客户端库,例如org.apache.httpcomponents
. 代码将如下所示:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.spiegel.de");
try
{
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (ClientProtocolException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
这是 Maven 工件:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>