Proxy address() in Java

address(): This method is available in the java.net.Proxy class of Java.

Syntax:

SocketAddress java.net.Proxy.address()

This method returns the socket address of the proxy, or null if it's a direct connection.

Parameters: NA

Returns: a SocketAddress representing the socket endpoint of the proxy.

Exceptions: NA

Approach

Java

package com.Proxy;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.UnknownHostException;

public class Proxyaddress {
    public static void main(String[] args) throws UnknownHostException {

        InetAddress addr = InetAddress.getByName("java.sun.com");
        int port = 80;
        SocketAddress sockaddr = new InetSocketAddress(addr, port);
        Proxy proxy = new Proxy(Type.HTTP, sockaddr);

        System.out.println(proxy.address());

    }
}

Output:

java.sun.com/23.54.82.226:80


Some other methods of Proxy class

Proxy(Type, SocketAddress)This method creates an entry representing a PROXY connection. 

address()This method returns the socket address of the proxy, or null if it's a direct connection.

Proxy ConstantsSome of the Proxy constants like Proxy.NO_PROXY, Type.DIRECT, etc.

equals(Object)This method compares this object against the specified object.

hashCode()This method returns a hashcode for this Proxy.

toString()This method constructs a string representation of this Proxy. This String is constructed by calling toString() on its type and concatenating " @ " and the toString() results from its address if its type is not DIRECT.

type()This method returns the proxy type.

No comments:

Post a Comment