Skip to content

Instantly share code, notes, and snippets.

@lizell
Last active March 2, 2020 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lizell/5088969 to your computer and use it in GitHub Desktop.
Save lizell/5088969 to your computer and use it in GitHub Desktop.
How to set a global proxy in Java when you can not control how your third party code communicates. Put this static block somewhere in a class that is loaded by your application.
static {
ProxySelector.setDefault(new ProxySelector() {
private final ProxySelector def = ProxySelector.getDefault();
@Override
public List<Proxy> select(final URI uri) {
if ("http".equalsIgnoreCase(uri.getScheme()) || "socket".equalsIgnoreCase(uri.getScheme())) {
if (uri.getHost().contains("<changeme>")) {
// Using proxy for <changeme> call
return Arrays.asList(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("192.168.111.1", 8889)));
}
}
return def.select(uri);
}
@Override
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
@gonzobrains
Copy link

I gotta give you props for the Commodore logo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment