How to Create an HTTP Proxy Server in Java
In today’s world, the proxy is the need for almost everyone. This tutorial demonstrates how to create an HTTP proxy server in Java.
Create an HTTP Proxy Server in Java
A proxy server is an intermediate approach between the client and server. This tutorial will demonstrate an approach to creating an HTTP proxy server in Java.
A proxy server has some advantages, which include:
- With Proxy Servers, we can create statistics about traffic.
- With Proxy Servers, we can update the content of the client.
- We can analyze the trouble network system reaction with Proxy Servers.
- We can use Proxy servers to control the limit of downloading and uploading.
- We can use Proxy servers to capture the traffic between the server and client.
Java supports proxy handlers for different protocols, for instance, HTTP, FTP, HTTPS, etc. ProxyHost and ProxyPort are used for an HTTP proxy server in Java.
Let’s try to implement an HTTP proxy server in Java:
package delftstack;
import java.io.*;
import java.net.*;
public class Proxy_Server {
public static void main(String[] args) throws IOException {
try {
String Proxy_Host = "Our Proxy Server";
int Remote_Port = 1025;
int Local_Port = 1026;
// Printing the start-up message
System.out.println(
"Starting proxy for " + Proxy_Host + ":" + Remote_Port + " on port " + Local_Port);
// start the server
Run_Server(Proxy_Host, Remote_Port, Local_Port);
} catch (Exception e) {
System.err.println(e);
}
}
public static void Run_Server(String Proxy_Host, int Remote_Port, int Local_Port)
throws IOException {
// Create a ServerSocket to listen connections
ServerSocket Server_Socket = new ServerSocket(Local_Port);
final byte[] Request = new byte[1024];
byte[] Reply = new byte[4096];
while (true) {
Socket Socket_Client = null, Socket_Server = null;
try {
// wait for a connection on the local port
Socket_Client = Server_Socket.accept();
final InputStream InputStreamClient = Socket_Client.getInputStream();
final OutputStream OutputStreamClient = Socket_Client.getOutputStream();
// Create the connection to the real server.
try {
Socket_Server = new Socket(Proxy_Host, Remote_Port);
} catch (IOException e) {
PrintWriter out = new PrintWriter(OutputStreamClient);
out.print("The Proxy Server could not connect to " + Proxy_Host + ":" + Remote_Port
+ ":\n" + e + "\n");
out.flush();
Socket_Client.close();
continue;
}
final InputStream InputStreamServer = Socket_Server.getInputStream();
final OutputStream OutputStreamServer = Socket_Server.getOutputStream();
// The thread to read the client's requests and to pass them
Thread New_Thread = new Thread() {
public void run() {
int Bytes_Read;
try {
while ((Bytes_Read = InputStreamClient.read(Request)) != -1) {
OutputStreamServer.write(Request, 0, Bytes_Read);
OutputStreamServer.flush();
}
} catch (IOException e) {
}
// Close the connections
try {
OutputStreamServer.close();
} catch (IOException e) {
}
}
};
// client-to-server request thread
New_Thread.start();
// Read server's responses and pass them to the client.
int Bytes_Read;
try {
while ((Bytes_Read = InputStreamServer.read(Reply)) != -1) {
OutputStreamClient.write(Reply, 0, Bytes_Read);
OutputStreamClient.flush();
}
} catch (IOException e) {
}
// Close the connection
OutputStreamClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (Socket_Server != null)
Socket_Server.close();
if (Socket_Client != null)
Socket_Client.close();
} catch (IOException e) {
}
}
}
}
}
The code above implements an HTTP proxy server in Java. The output for successful connection is:
Starting proxy for Our Proxy Server:1025 on port 1026
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook