Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles Class in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are already pre-installed in Windows in a type of program or application. It is automatically saved in the settings section. You can also make customs for which site you want to use a proxy. InetSocketAddress class from package implements IP socket address(combination of IP address and port number). The objects of this class are immutable and can be used for binding, connecting purposes

The class represents a proxy setting which is basically a type and a socket address. The class contains a special field that is the no proxy field. It is written as Proxy NO_PROXY; this setting tells the protocol handler not to use any proxy setting and represents a Direct connection. 

Syntax: Class Declaration

public class Proxy extends Object

Let us do discuss the constructor of this class prior to jumping onto the methods

Proxy(Proxy.Type type, SocketAddress sa)

Methods of the class are as follows

Method Name Description
address() Returns the socket address of the proxy or returns null for a direct connection.
equals() Compares both the proxy objects and returns true only if the type and address are same.
hashCode() Returns a hashcode for the proxy.
toString() Returns a string representation of the proxy.
type() Returns the type of the proxy object.


  1. First, we have created a Socket Address in order to use with the proxy object.
  2. Then created a HTTP type proxy with that address.
  3. Then we have created a URL connection with the proxy we created.




// Java Program to illustrate Proxy Class
// of package
// Importing input output classes
// importing java net package to use address and url fields
// importing the java proxy package
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
        // Creating  socket address with port 8080
        // by creating object of SocketAddress class
        SocketAddress addr = new InetSocketAddress(
            "", 8080);
        // Creating proxy object of type HTTP with
        // address addr using the class constructor
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
        // Try block to check for exceptions
        try {
            // Creating URL connection using the above proxy
            // by creating an object of URL class
            URL url = new URL("");
            // Now setting the connecting by
            // creating an object of URLConnection class
            URLConnection conn = url.openConnection(proxy);
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Print the line number here exception occurred
            // using the printStackTrace() method
            // Display message only illustrating
        // Printing Proxy Type
        // using type() method
        System.out.println("Proxy Type: " + proxy.type());
        // Printing Proxy Address
        // using address() method
        System.out.println("Proxy Address: "
                           + proxy.address());
        // Printing Proxy Hashcode
        // using hashCode() method
        System.out.println("Proxy HasHCode: "
                           + proxy.hashCode());
        // Printing Proxy String representation
        // using toString() method
        System.out.println("Proxy String: "
                           + proxy.toString());


My Personal Notes arrow_drop_up
Last Updated : 16 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials