java.net.ProxySelector Class in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report ProxySelector determines which resource has to be requested via proxy as a result return List<Proxy> Methods of ProxySelector class : MethodDescriptionconnectFailed()This method is invoked when failed to establish a connectiongetDefault()This method is used for retrieving the system-wide ProxySelectorselect()This method returns Proxy to access resourcesetDefault() This method is used to set or unset the system-wide ProxySelector Illustration: Sample Code for logic Java // Java Program to illustrate ProxySelector Class // of java.net package // only creating methods here // Importing standard input output classes import java.io.IOException; // Importing classes from java.net package import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI; // Importing List and ArrayList as utility classes from // java.util package import java.util.ArrayList; import java.util.List; // Class 1 // Helper class extending ProxySelector class public class PrivateDataProxy extends ProxySelector { // According to API we need to return List<Proxy> // even if we return only one element, so // Creating List class object of Proxy type private final List<Proxy> noProxy = new ArrayList<>(); private final List<Proxy> proxies = new ArrayList<>(); // Constructor of this class public PrivateDataProxy() { // If no proxy required to access resource // use Proxy.NO_PROXY noProxy.add(Proxy.NO_PROXY); // Creating InetSocketAddress, and // secure.connection.com doesn't exist 443 is an // https port InetSocketAddress inetSocketAddress = new InetSocketAddress("secure.connection.com", 443); // Now creating http proxy Proxy proxy = new Proxy(Proxy.Type.HTTP, inetSocketAddress); // Finally adding proxy into proxy list proxies.add(proxy); } // Method 1 of this class //@Override public List<Proxy> select(URI uri) { if (uri.getPath().startsWith("/confidential")) { // If URI path starts with '/confidential' then // use proxy server return proxies; } // If url don't start with '/confidential' then // no need in proxy return noProxy; } // Method 2 of this class // @Override public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) { // Properly handle connection failing } } Implementation: Using custom ProxySelector Java // Java Program to illustrate ProxySelector Class // of java.net package // Using custom ProxySelector // Importing required classes from respective packages import java.io.IOException; import java.net.Proxy; import java.net.ProxySelector; import java.net.URISyntaxException; import java.net.URL; import java.util.List; // Main class public class ProxySelectorDemo { // Main driver method public static void main(String[] args) throws URISyntaxException, IOException { // Passing the string uri PrivateDataProxy privateDataProxy = new PrivateDataProxy(); // The setting the system-wide proxy selector ProxySelector.setDefault(privateDataProxy); // Print the default value // using getDefault() method System.out.println("Default value: " + ProxySelector.getDefault()); // Display message only System.out.println( "Getting proxy for /confidential"); // Passing the string URL String confidentialUrl = "https://download.cnet.com/confidential"; // Now, calling the constructor of the URL class URL confidential = new URL(confidentialUrl); // Requiring an proxy for url List<Proxy> confidentialProxies = privateDataProxy.select(confidential.toURI()); // Show the proxy that was selected System.out.println("Proxy to use : " + confidentialProxies.get(0)); // Display message only System.out.println( "Getting proxy for /non-confidential"); // passing the string URL // Custom URL as input String nonConfidentialURL = "https://download.cnet.com/non-confidential"; // Now, calling the constructor of the URL class URL nonConfidential = new URL(nonConfidentialURL); // Requiring an proxy for URL List<Proxy> nonConfidentialProxies = privateDataProxy.select( nonConfidential.toURI()); // Display the proxy that was selected System.out.println("Proxy to use : " + nonConfidentialProxies.get(0)); } } Output: Default value: entity.PrivateDataProxy@5cad8086 Getting proxy for /confidential Proxy to use : HTTP @ secure.connection.com:443 Getting proxy for /non-confidential Proxy to use : DIRECT Comment More infoAdvertise with us A alijakparovkz Follow Improve Article Tags : Java Java-Classes Java-net-package Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like