Java 中的 UnknownHostException [英] UnknownHostException in Java

查看:70
本文介绍了Java 中的 UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码连接到一个 url.我在我的办公系统中执行它时收到此错误.但在我的个人笔记本电脑上它正在工作.我认为它必须与代理做一些事情.我有代理详细信息.但是如何在下面的代码中指定它??

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

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

推荐答案

来自 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.

这篇关于Java 中的 UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆