UnknownHostException异常与Apache了HTTPClient [英] UnknownHostException with Apache HTTPClient

查看:373
本文介绍了UnknownHostException异常与Apache了HTTPClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hithere。

我试图不要使用Apache的HttpClient库的DefaultHttpClient一个URL执行GET。

I am trying to perform a GET against a URL using DefaultHttpClient of Apache's HTTPClient library.

下面是我的code:

    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,我没有问题。但是,如果我用一个URL使用相对路径,如www.google.ie/intl/en/ads/,它失败。我从上面的 httpclient.execute()方法抛出一个UnknownHostException。它只有相对URL发生,我不知道为什么。有没有人任何输入,为什么?非常感谢

If I pass a URL such as "www.google.ie", I have no problems. However, if I use a URL with a relative path such as "www.google.ie/intl/en/ads/", it fails. I get an UnknownHostException thrown from the httpclient.execute() method above. It only happens with relative URLs and I'm not sure why. Has anyone any input as to why? Many thanks

推荐答案

主持人是 www.google.com 剩下的不是一台主机,但路径(或映射)在主机内。这应该转到新HTTPGET(_这里_)

The host is www.google.com the rest is not a host but a path (or mapping) within the host. This should go to the new HttpGet("_HERE_")

所以,你将有:

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

这篇关于UnknownHostException异常与Apache了HTTPClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆