我使用下面的代码连接到网址。我在我的办公系统中执行它时遇到此错误。但在我的个人笔记本电脑上它正在工作。我认为它必须与代理做一些事情。我有代理详细信息。但是如何在下面的代码中指定它?
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();
}
}
}
答案 0 :(得分:3)
来自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);
然后使用现有代码执行该方法。