正しいエンコーディングを設定する必要があります。エンコーディングは HTTP ヘッダーで確認できます。
Content-Type: text/html; charset=ISO-8859-1
これは (X)HTML ドキュメントで上書きされる可能性があります。HTML 文字エンコーディングを参照してください。
Web ページをエラーなしで解析するには、さまざまな追加の問題を考慮する必要があると想像できます。ただし、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>