ProxySelector connectFailed(URI, SocketAddress, IOException) in Java

connectFailed(URI, SocketAddress, IOException): This method is available in the java.net.ProxySelector class of Java.

Syntax:

void java.net.ProxySelector.connectFailed(URI uri, SocketAddress sa, IOException ioe)

This method takes three arguments. This method is called to indicate that a connection could not be established to a proxy/socks server. An implementation of this method can temporarily remove the proxies or reorder the sequence of proxies returned by select(URI), using the address and the IOException caught when trying to connect.

Parameters: Three parameters are required for this method.

uri: The URI that the proxy at sa failed to serve.

sa: The socket address of the proxy/SOCKS server.

ioe: The I/O exception is thrown when the connection failed.

Returns: NA

Throws:

1. IllegalArgumentException - if either argument is null

Approach 1: When no exception

Java

package com.ProxySelector;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

public class ProxySelectorconnectFailed {
    public static void main(String[] args) throws URISyntaxException, UnknownHostException {
        ProxySelector proxySelector = ProxySelector.getDefault();

        URI uri = new URI("http://localhost:8080");
        InetAddress addr = InetAddress.getByName("java.sun.com");
        int port = 80;
        SocketAddress sa = new InetSocketAddress(addr, port);
        IOException ioe = new IOException();
        proxySelector.connectFailed(uri, sa, ioe);
        System.out.println("Successfully connectFailed");
    }
}

Output:

Successfully connectFailed


Approach 2: IllegalArgumentException 

Java

package com.ProxySelector;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

public class ProxySelectorconnectFailed {
    public static void main(String[] args) throws URISyntaxException, UnknownHostException {
        ProxySelector proxySelector = ProxySelector.getDefault();

        URI uri = new URI("http://localhost:8080");
        InetAddress addr = InetAddress.getByName("java.sun.com");
        int port = 80;
        SocketAddress sa = new InetSocketAddress(addr, port);
        IOException ioe = null;
        proxySelector.connectFailed(uri, sa, ioe);
        System.out.println("Successfully connectFailed");
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Arguments can't be null. at java.base/sun.net.spi.DefaultProxySelector.connectFailed(DefaultProxySelector.java:330) at com.ProxySelector.ProxySelectorconnectFailed.main(ProxySelectorconnectFailed.java:21)


Some other methods of ProxySelector class

connectFailed(URI, SocketAddress, IOException)This method is called to indicate that a connection could not be established to a proxy/socks server.

getDefault()This method gets the system-wide proxy selector.

ProxySelector.of(InetSocketAddress) This method returns a ProxySelector which uses the given proxy address for all HTTP and HTTPS requests. If the proxy is null then proxying is disabled.

select(URI)This method selects all the applicable proxies based on the protocol to access the resource with and a destination address to access the resource at.

ProxySelector.setDefault(ProxySelector)This method sets (or unsets) the system-wide proxy selector.

No comments:

Post a Comment