1

Surprisingly I couldn't find a working solution for such a trivial question.

In a Java servlet I need to connect to external web site and in doGet method I'm using

URL website = new URL(websiteUrl);

for this.

This code works on my PC but fails to connect when running on production Linux application server.

I checked Firefox network settings on the server and it is set to use system proxy. Using echo $http_proxy command in Linux terminal I read the proxy settings and changed my code to:

URL website = new URL("http", proxy, Integer.parseInt(proxyPort), websiteUrl);

setting proxy settings hard coded.

Now it works but obviously I wouldn't like to hard-code the proxy settings but to read it dynamically.

That is where I stumbled as none of the methods I found on the Internet worked for me.

System.getProperty("http.proxyHost")
System.getProperty("http_proxy")

ProxySelector methods or adding

System.setProperty("java.net.useSystemProxies", "true");

brought no result.

Is there a working solution? Any idea why those common solutions do not work in my case?

I'm running WebLogic 10.3.5/JRockit on RHEL 6. Thanks.

4

2 に答える 2

0
System.setProperty("java.net.useSystemProxies", "true");
System.out.println("detecting proxies");
List l = null;
try {
    l = ProxySelector.getDefault().select(new URI("http://foo/bar"));
} 
catch (URISyntaxException e) {
    e.printStackTrace();
}
if (l != null) {
    for (Iterator iter = l.iterator(); iter.hasNext();) {
        java.net.Proxy proxy = (java.net.Proxy) iter.next();
        System.out.println("proxy hostname : " + proxy.type());

        InetSocketAddress addr = (InetSocketAddress) proxy.address();

        if (addr == null) {
            System.out.println("No Proxy");
        } else {
            System.out.println("proxy hostname : " + addr.getHostName());
            System.setProperty("http.proxyHost", addr.getHostName());
            System.out.println("proxy port : " + addr.getPort());
            System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
        }
    }
}
于 2013-11-12T16:13:36.360 に答える