How can I use an HTTP proxy for a JAX-WS request without setting a system-wide property?

I recommend using a custom ProxySelector. I had the same problem and it works great and is super flexible. It’s simple too.

Here’s my CustomProxySelector:

import org.hibernate.validator.util.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

 * So the way a ProxySelector works is that for all Connections made,
 * it delegates to a proxySelector(There is a default we're going to
 * override with this class) to know if it needs to use a proxy
 * for the connection.
 * <p>This class was specifically created with the intent to proxy connections
 * going to the allegiance soap service.</p>
 * @author Nate
class CustomProxySelector extends ProxySelector {

    private final ProxySelector def;

    private Proxy proxy;

    private static final Logger logger = Logger.getLogger(CustomProxySelector.class.getName());

    private List<Proxy> proxyList = new ArrayList<Proxy>();

     * We want to hang onto the default and delegate
     * everything to it unless it's one of the url's
     * we need proxied.
    CustomProxySelector(String proxyHost, String proxyPort) {
        this.def = ProxySelector.getDefault();
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, (null == proxyPort) ? 80 : Integer.valueOf(proxyPort)));

    public List<Proxy> select(URI uri) {"Trying to reach URL : " + uri);
        if (uri == null) {
            throw new IllegalArgumentException("URI can't be null.");
        if (uri.getHost().contains("allegiancetech")) {
  "We're trying to reach allegiance so we're going to use the extProxy.");
            return proxyList;

    * Method called by the handlers when it failed to connect
    * to one of the proxies returned by select().
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        logger.severe("Failed to connect to a proxy when connecting to " + uri.getHost());
        if (uri == null || sa == null || ioe == null) {
            throw new IllegalArgumentException("Arguments can't be null.");
        def.connectFailed(uri, sa, ioe);

Leave a Comment