2

I am using below code to connect to a url. Iam getting this error while executing it in my office system. but on my personal laptop it is working. I think it has to do something with the proxy. i have the proxy details . but how to specify it in the below code??

java.net.UnknownHostException: www.google.com

import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.methods.GetMethod;

public class test {

public static void main(String args[]) throws Exception {
  HttpClient client = new HttpClient();                   
   GetMethod method = new GetMethod("http://www.google.com");
 try{
      client.executeMethod(method);
  }catch(Exception e) { 
      System.err.println(e); 
  }finally { 
      method.releaseConnection(); 
  }
 }
}
2
  • What version of HttpClient are you using? Commented Jun 17, 2011 at 9:59
  • You have more than ten questions without an accepted answer. ;) Commented Jun 17, 2011 at 9:59

1 Answer 1

3

From KodeJava

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.kodejava.org");
HostConfiguration config = client.getHostConfiguration();

config.setProxy(PROXY_HOST, PROXY_PORT);
String username = "guest"; String password = "s3cr3t";
Credentials credentials = new UsernamePasswordCredentials(username, password);
AuthScope authScope = new AuthScope(PROXY_HOST, PROXY_PORT);
client.getState().setProxyCredentials(authScope, credentials);

And then use your existing code to execute the method.

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.