UnknownHostException in Java
I am using below code to connect to a url. Iam getting this error while executing it in my office sy开发者_如何学编程stem. 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();
}
}
}
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.
精彩评论