0

Hithere.

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

Here is my 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();

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

Joeblackdev
  • 7,217
  • 24
  • 69
  • 106

1 Answers1

3

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_")

So you will have:

new HttpGet("/intl/en/ads/");
Jan Zyka
  • 17,460
  • 16
  • 70
  • 118