Java HttpURLConnection java.net.UnknownHostException

可紊 提交于 2019-12-12 04:37:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!