嗨,您好。

我正在尝试使用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”之类的URL,则不会有任何问题。但是,如果我使用带有相对路径的URL,例如“ www.google.ie/intl/zh-CN/ads/”,它将失败。我从上面的httpclient.execute()方法中抛出了UnknownHostException。它仅在相对URL中发生,我不确定为什么。有没有人输入任何关于为什么的信息?非常感谢

最佳答案

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

因此,您将拥有:

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

10-08 04:49