2

I am trying to make a simple GET request for a website, but I am getting unknown host exception.

Given below is my code--

     DefaultHttpClient client = new DefaultHttpClient();
     HttpHost targetHost=null;
     targetHost= new HttpHost("google.com/", 80, "http");
     HttpGet httpget = new HttpGet("about-us.html");
     BasicHttpContext localcontext = new BasicHttpContext();
     try {
        HttpResponse response = client.execute(targetHost, httpget, localcontext);
1
  • 7
    Why do you have a slash on the end of the hostname? Commented Jan 27, 2012 at 19:31

1 Answer 1

7

It looks like you have a simple problem here.

The URL for your 'HttpHost' object is malformed. You need to drop the '/' from "google.com/". It should work after that. I used your code with that single modification & it worked.

DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("google.com", 80, "http"); 
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localContext = new BasicHttpContext();
HttpResponse response = null;

try { response = client.execute(targetHost, httpget, localContext); 
      System.out.println(response.getStatusLine()
}
catch(Exception e){
    // Enter error-handling code here.
}
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.