Java HttpURLConnection java.net.UnknownHostException [英] Java HttpURLConnection java.net.UnknownHostException
问题描述
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
第二行抛出java.net.UnknownHostException
.
我当时在公司的内部网络中,希望我可以帮助另一个也在同一内部网络中的人访问该网站,我只想从URL中读取内容并将其提供给客户端,有人在这方面有帮助吗?
The second line throws java.net.UnknownHostException
.
I was in a internal network in my company, and I hope I can help another one who's also in the same internal network with me to visit the website, and I just want to read content from the URL and give the content to the client side, is there any one help me on this?
推荐答案
公司通常会向外界提供代理服务器.尝试在代理中配置代理数据类,然后使用代理打开连接.
Companys normally have a proxy server to the outside world. Try to configure the proxy data in a Proxy class and open the connection with proxy.
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(<ProxyUrl>,<ProxyPort>));
conn = new URL(urlString).openConnection(proxy);
如果您的代理具有密码验证,则必须在Authenticator中设置登录数据:
If your proxy has a passwort authentication you have to set the log in data in an Authenticator:
Authenticator authenticator = new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return (new PasswordAuthentication(<ProxyUser>,<ProxyPW>.toCharArray()));
}
};
Authenticator.setDefault(authenticator);
这篇关于Java HttpURLConnection java.net.UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!