繁体   English   中英

Apache HTTPClient的UnknownHostException

[英]UnknownHostException with 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();

如果我传递诸如“ www.google.ie”之类的网址,则不会有任何问题。 但是,如果我使用具有相对路径的URL,例如“ www.google.ie/intl/zh-CN/ads/”,它将失败。 我从上面的httpclient.execute()方法中抛出了UnknownHostException。 它仅在相对URL中发生,我不确定为什么。 有任何人输入为什么吗? 非常感谢

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

因此,您将拥有:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM