gpt4 book ai didi

java - Java 中的 UnknownHostException

转载 作者:行者123 更新时间:2023-12-04 06:22:29 28 4
gpt4 key购买 nike

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

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

然后使用您现有的代码来执行该方法。

关于java - Java 中的 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6384135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com