UnknownHostException in Java

怎甘沉沦 提交于 2020-01-15 07:26:06

问题


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(); 
  }
 }
}

回答1:


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.



来源:https://stackoverflow.com/questions/6384135/unknownhostexception-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!