Skip to content

Instantly share code, notes, and snippets.

@ah8r
Forked from lizell/globalProxy.java
Last active April 18, 2018 16:42
Show Gist options
  • Save ah8r/3adff8080e40de04658908c51e755ec3 to your computer and use it in GitHub Desktop.
Save ah8r/3adff8080e40de04658908c51e755ec3 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) {
return Arrays.asList(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 8889)));
}
@Override
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment