Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class ProxyChange {
- public static void change(List<String> ignoreHosts, String proxyAddress, int port) {
- List<String> ignore = new ArrayList<>();
- for (String item : ignoreHosts) ignore.add(item.toLowerCase().trim());
- Collections.sort(ignore);
- ProxySelector.setDefault(new ProxySelector() {
- private final ProxySelector def = ProxySelector.getDefault();
- @Override
- public List<Proxy> select(final URI uri) {
- String scheme = uri.getScheme();
- String host = uri.getHost();
- if (Collections.binarySearch(ignore, host) < 0 && (scheme.startsWith("http") || scheme.startsWith("socket") || scheme.startsWith("ftp"))) {
- return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddress, port)));
- } else {
- return def.select(uri);
- }
- }
- @Override
- public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement