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.

ILya Cyclone
  • 806
  • 6
  • 16

2 Answers2

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()));
        }
    }
}
constantlearner
  • 5,157
  • 7
  • 42
  • 64
  • Tried this: detecting proxies proxy hostname : DIRECT No Proxy – ILya Cyclone Nov 12 '13 at 16:18
  • `l = ProxySelector.getDefault().select(new URI("http://google.com/")); `- same result – ILya Cyclone Nov 12 '13 at 16:22
  • http://stackoverflow.com/questions/1499970/how-do-i-configure-proxy-settings-for-java-in-solaris-to-handle-proxy-auto-confi/4877497#4877497 – constantlearner Nov 12 '13 at 16:24
  • Probably I couldn't understand usage examples correctly but in this example code `ProxySearch proxySearch = ProxySearch.getDefaultProxySearch(); ProxySelector myProxySelector = proxySearch.getProxySelector(); ProxySelector.setDefault(myProxySelector);` myProxySelector is null in my case. – ILya Cyclone Nov 13 '13 at 10:36
-1

You likely have no system wide defiend proxy, but Firefox has one configured in its settings. Hence the proxy detection code correctly points out: no proxy.

Angel O'Sphere
  • 2,642
  • 20
  • 18