Apache HTTPClient的UnknownHostException

时间:2011-03-21 13:03:19

标签: java apache exception httpclient

Hithere。

我正在尝试使用Apache的HTTPClient库的DefaultHttpClient对URL执行GET。

这是我的代码:

    public String getHTML(String url) throws IOException, ClientProtocolException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        HttpHost targetHost = new HttpHost(url);
        HttpGet httpGet = new HttpGet("/");
        HttpResponse response = httpclient.execute(targetHost, httpGet);
        HttpEntity entity = response.getEntity();

如果我传递“www.google.ie”等网址,我就没有问题。但是,如果我使用带有相对路径的网址,例如“www.google.ie/intl/en/ads/”,则会失败。我从上面的httpclient.execute()方法中抛出了一个UnknownHostException。它只发生在相对URL上,我不知道为什么。有谁输入为什么?非常感谢

1 个答案:

答案 0 :(得分:3)

主机为www.google.com,其余主机不是主机,而是主机内的路径(或映射)。这应该转到new HttpGet("_HERE_")

所以你会:

new HttpGet("/intl/en/ads/");