2011-03-21 74 views
0

Hithere。UnknownHostException与Apache HTTPClient

我正在尝试使用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(); 

如果我通过一个URL,如 “www.google.ie”,我没有问题。但是,如果我使用具有诸如“www.google.ie/intl/en/ads/”之类的相对路径的网址,则该网址会失败。我收到了上面httpclient.execute()方法抛出的UnknownHostException。它只发生在相对的网址,我不知道为什么。有没有人为什么输入?非常感谢

+0

对不起,我发现这个问题。我不得不将相对路径传递给HTTPGet。道歉。 – Joeblackdev 2011-03-21 13:07:29

回答

3

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

所以,你将有:

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

现货!我刚刚发现。非常感谢您的回复。 – Joeblackdev 2011-03-21 13:07:56