Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article Class in Java

  • Difficulty Level : Basic
  • Last Updated : 16 May, 2021

ProxySelector determines which resource has to be requested via proxy as a result return List<Proxy>

Methods of ProxySelector class : 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

connectFailed()This method is invoked when failed to establish a connection
getDefault()This method is used for retrieving the system-wide ProxySelector
select()This method returns Proxy to access resource
setDefault() This method is used to set or unset the system-wide ProxySelector

Illustration: Sample Code for logic


// Java Program to illustrate ProxySelector Class
// of package
// only creating methods here
// Importing standard input output classes
// Importing classes from package
// 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
        // Creating  InetSocketAddress, and
        // doesn't exist 443 is an
        // https port
        InetSocketAddress inetSocketAddress
            = new InetSocketAddress("",
        // Now creating http proxy
        Proxy proxy
            = new Proxy(Proxy.Type.HTTP, inetSocketAddress);
        // Finally adding proxy into proxy list
    // Method 1 of this class
    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 Program to illustrate ProxySelector Class
// of package
// Using custom ProxySelector
// Importing required classes from respective packages
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
        // Print the default value
        // using getDefault() method
        System.out.println("Default value: "
                           + ProxySelector.getDefault());
        // Display message only
            "Getting proxy for /confidential");
        // Passing the string URL
        String confidentialUrl
            = "";
        // Now, calling the constructor of the URL class
        URL confidential = new URL(confidentialUrl);
        // Requiring an proxy for url
        List<Proxy> confidentialProxies
        // Show the proxy that was selected
        System.out.println("Proxy to use : "
                           + confidentialProxies.get(0));
        // Display message only
            "Getting proxy for /non-confidential");
        // passing the string URL
        // Custom URL as input
        String nonConfidentialURL
            = "";
        // Now, calling the constructor of the URL class
        URL nonConfidential = new URL(nonConfidentialURL);
        // Requiring an proxy for URL
        List<Proxy> nonConfidentialProxies
        // Display the proxy that was selected
        System.out.println("Proxy to use : "
                           + nonConfidentialProxies.get(0));


Default value: entity.PrivateDataProxy@5cad8086
Getting proxy for /confidential
Proxy to use : HTTP @
Getting proxy for /non-confidential
Proxy to use : DIRECT

My Personal Notes arrow_drop_up
Recommended Articles
Page :