问题
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
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?
回答1:
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);
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);
来源:https://stackoverflow.com/questions/25992586/java-httpurlconnection-java-net-unknownhostexception