1

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

1
  • Sorry, I found the problem. I had to pass the relative path into the HTTPGet. Apologies. Commented Mar 21, 2011 at 13:07

1 Answer 1

4

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/");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.