Getting proxy server details in webstart app

I had a devil of a job finding a way to do this.

A couple of apps I have recently worked on needed to make an apache httpclient or xmlrpc connection back to the server once the webstart app started. Although webstart clearly has the proxy server info (which it must use when downloading the application jars from the server), it is not immediately apparent how to access this from the app itself. The app then fails when trying to call back to the server.

I thought the proxyHost and proxyPort system properties might contain the required information – but this is not the case. In fact this information is not available from system properties at all.

Well the trick is to get the http proxy details using the java.net.ProxySelector class.

String theURIToConnectTo="http://www.yourServerURIHere.com"

String proxyHost, proxyPort;
java.util.List<Proxy> proxies = java.net.ProxySelector.getDefault().select(new java.net.URI(theURIToConnectTo));
for ( Proxy proxy : proxies ) {

    if ( proxy.type() == Proxy.Type.HTTP ) {
        SocketAddress proxyAddress = proxy.address();
        if ( proxyAddress instanceof InetSocketAddress) {
             proxyHost = ((InetSocketAddress)proxyAddress).getHostName();
             proxyPort = String.valueOf(((InetSocketAddress)proxyAddress).getPort());
        }
        break;
    }
 }

This still won’t handle the case where the proxy server requires username and password for authentication.
I don’t have a suitably authenticating proxy to test with right now – anybody know how to handle that?





You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

2 Responses to “Getting proxy server details in webstart app”

  1. Before you run the above code I’ve found I had to set

    System.setProperty(“java.net.useSystemProxies”, “true”);

    otherwise the code above does not give me any proxies. Just gives back DirectConnection as the proxy-type.

    If you just want to use whatever the system proxy setting is and your are just using the standard java libraries for connection, setting the above property seems to enough.

    HttpClient 3 is not Java 5 aware however and doesn’t pick up the Proxy setting (I think HttpClient 4 will do this in future). So you have to run the above code to extract out the proxyHost and proxyPort to use.

  2. That’s really good to know, thanks for the comment Mumin!

Leave a Reply