Java HTTP Proxy Server3 May 2025 | 7 min read In today's corporate development environment, we have to deal with proxies, usually as system administrators. In most cases, the application will be configured to the system's default setting, but if you want very tight control over your application like proxy setting, for such cases, Java allows APIs. The proxy server is like an intermediate system between the client-side application and other servers. In an enterprise application, which is used to provide control over the user's content across network boundaries. The below image demonstrates the behavior of the proxy server: ![]() In this topic, we will understand how to connect through proxy servers in Java. We will follow two approaches to create a connection along with proxy servers in Java.
The Proxy API is available since Java 5.0. However, the old approaches are still valid and can be used with our projects. But the Proxy class approach is more effective and customizable. Advantages of Using Proxy ServersThe proxy servers are useful in the following cases:
System PropertiesJava supports proxy handlers for different protocols such as FTP, HTTP, HTTPS, and SOCKs. We can define an individual proxy for an individual handler as the hostname and port number. The following system properties are available in Java proxy configuration:
Note: We may start or end a host pattern using a wildcard character ("*") in the case of nonProxyHosts. But on the Windows platform, it is necessary to remove the "|" delimiter. The list of all available proxy system properties can be found Here.Using a Global SettingJava provides several system properties that we have discussed above to configure the JVM-wide behavior. These properties are easy to implement for a particular use case. We can also set the necessary properties using the command line while invoking the JVM. There is an alternative way to do so, and they can be set by calling the System.setProperty() method at runtime. Let's understand how to set them using the command line: Set Proxy Using the Command LineWe can also set the proxy properties using the command line arguments. To define the proxies using the command line, pass the settings as system properties as follows: By starting process in this way, we can use openConnection() method on the URL without doing any further effort as follows: Set Proxy Using the System.setProperty() MethodIf we face difficulty while using the command line, there is an alternative way to do so by using the System.setProperty() method. To set the Proxy using this method, define it within our program as follows: Later, we can unset the system properties, and if we want, then they will be removed from our application. To unset the system property, make it null by defining it within our program as follows: The Global setting has some limitations; here, the concept of Proxy API came into the picture. Let's discuss the limitations of the Global setting: Limitations of Global Configuration ApproachThe Global Configuration approach is the simplest way to define the proxy, but this approach has some limitations. This approach provides the implementation on the JVM-wide, so the settings define for a particular protocol are active for the life of the JVM or until we unset them manually. To get over this limitation, it may be attractive to flip the settings on and off, if needed. But, it would be necessary to ensure the measures to protect against concurrency issues in a multi-threaded program. So, as an alternative, the Proxy API is more efficient and provides more control over proxy configuration. Set Proxy Using the Proxy APIThe Java Proxy class provides a convenient way to configure proxies on the basis of the connection. If we set the proxy using the Proxy class, it will override the existing JVM-wide proxy setting. There are three types of proxies that can be defined by using Proxy.Type() method of Proxy class:
Let's understand these proxies: 1) HTTP ProxyTo use an HTTP proxy, wrap the SocketAddress instance with proxy and provide type as Proxt.Type.HTTP. Now, we can simply pass the proxy instance to URLConnection.openConnection(). Consider the below code: Now, we'll connect to URL_STRING but then route that connection through a proxy server hosted at 127.0.0.1:3020. 2) DIRECT ProxyThe Direct Proxy is useful for connecting directly to a host. In this case, we have to explicitly bypass a proxy that may be configured globally by using the static "proxy.NO_PROXY" instance. Internally, the Proxy API creates a new proxy instance using the Proxy.Type.Direct type. Consider the below code: Basically, if there is no globally configured proxy, then this will work the same as calling openConnection() with no arguments. 3) Socks ProxyThe Socks proxy works in a similar way to HTTP variant while dealing with URLConnection. In Socks proxy, first we wrap a SocketAddress instance with a Proxy using the Proxy.Type.SOCKS type. After that, the Proxy instance is passed to URLConnection.openConnection. Consider the below code: We can also use a SOCKs proxy when connecting to a TCP socket. In order to do so, we need to use the Proxy instance to create a Socket. After that, the destination SocketAddress instance is passed to Socket.connect() method. Consider the below code: Java Program to Create a Simple Proxy ServerTestProxyServer.java: Output: ![]() Next TopicFind-triplets-with-zero-sum-in-java |
Jumping Number in Java
In this section, we will learn what is jumping number and also create Java programs to check if the given number is a jumping number or not. The jumping number program frequently asked in Java coding tests and academics. Jumping Number A number N called a jumping number...
7 min read
Stream findFirst() Method in Java
The Stream findFirst() method returns an Optional describing the 1st element of the stream, or an Optional, which has to be empty if the stream is empty. Syntax: Optional<T> findFirst() Here, Optional is the container object that can or cannot fetch a non-null value. T is the type of...
4 min read
Java Throwable.setStackTrace() Method
The stack trace elements are set to the throwable object using the Throwable class's setStackTrace(StackTraceElement[] stackTrace) method. getStackTrace() returns this stack trace, and printStackTrace() and related methods print it. By using the method, a user can override the default stack trace that is either deserialized when...
4 min read
How to Get an Environment Variable in Java
Each programming language for computers has variables and constants that allot particular memory addresses containing data that the programs can utilize. While the value of a variable can be altered, constant values cannot. The key/value combination known as an environment variable is one whose value is...
3 min read
Digit Extraction in Java
In the world of programming, dealing with numbers is a fundamental task. Often, we need to manipulate individual digits within a number for various applications like cryptography, data validation, or mathematical operations. The process is known as digit extraction. In this section we will explore different...
3 min read
Difference Between Static and Dynamic Dispatch in Java
Java, a versatile and widely-used programming language, employs various mechanisms for method dispatch, a process that determines which implementation of a method should be executed in response to a method call. Two primary methods of dispatch in Java are static dispatch and dynamic dispatch. Understanding the...
4 min read
compareToIgnoreCase Java
In Java, the method compareToIgnoreCase() belongs to the String class that belong to java.lang package. It is used for comparing any two strings by ignoring the lower- and upper-case differences. The method does the comparison of strings using the Unicode value of each character present in...
5 min read
Use of this Keyword in Java
In Java, this keyword is one of the most frequently used keywords in object-oriented programming. This variable serves as a reference to the current object of the class in which it is utilized. When an object's constructor or method is invoked, the object is represented by...
7 min read
JDoodle Java
In today's world, everything is instant and fast forwarded. Online compilers accessible via internet are very useful for programmers who are trying to learn a new programming language but have not ready to install the necessary software setups. In this section, we will discuss about JDoodle...
3 min read
Java Calculate Average of List
In Java, List is a linear data structure that store the ordered collection of data. It also accepts the duplicate values but preserve the insertion order. Sometimes, it is required to find the minimum and maximum element of the list, sum, and average of the list,...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

