June 221st, 2023
How to Build a Super Simple HTTP proxy in Java ?

Its quite easy to build a rudimentary proxy server in java.

Here's a step-by-step guide on creating a simple HTTP proxy server using Java:

Step 1: Set up the Project Create a new directory for your project and navigate into it in your terminal or command prompt.

Step 2: Create a New Java Class Create a new Java class in your project directory. You can name it whatever you like. In this example, let's name it SimpleProxyServer.

Step 3: Implement the Proxy Server Open the SimpleProxyServer.java file in your preferred Java IDE or text editor and paste the following code:


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleProxyServer {
    private static final int PORT = 9097;

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            System.out.println("Now serving at <http://localhost>:"   PORT);

            while (true) {
                Socket clientSocket = serverSocket.accept();
                Thread thread = new Thread(() -> handleClientRequest(clientSocket));
                thread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void handleClientRequest(Socket clientSocket) {
        try {
            InputStream clientIn = clientSocket.getInputStream();
            OutputStream clientOut = clientSocket.getOutputStream();

            byte[] buffer = new byte[4096];
            int bytesRead;

            // Read the client's request
            bytesRead = clientIn.read(buffer);
            String request = new String(buffer, 0, bytesRead);

            // Extract the target URL from the request
            String targetUrl = extractTargetUrl(request);

            // Open a connection to the target server
            Socket targetSocket = new Socket(targetUrl, 80);
            InputStream targetIn = targetSocket.getInputStream();
            OutputStream targetOut = targetSocket.getOutputStream();

            // Forward the client's request to the target server
            targetOut.write(request.getBytes());
            targetOut.flush();

            // Forward the target server's response to the client
            while ((bytesRead = targetIn.read(buffer)) != -1) {
                clientOut.write(buffer, 0, bytesRead);
                clientOut.flush();
            }

            // Close the sockets
            targetSocket.close();
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String extractTargetUrl(String request) {
        String[] lines = request.split("\\r\\n");
        String[] requestLine = lines[0].split(" ");
        return requestLine[1];
    }
}

Here's a breakdown of specific parts of the code to aid in understanding:

Server Setup:

ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Now serving at <http://localhost>:"   PORT);

These lines create a ServerSocket instance and bind it to the specified port. The server socket listens for incoming client connections on that port.

Handling Client Requests:

Socket clientSocket = serverSocket.accept();
Thread thread = new Thread(() -> handleClientRequest(clientSocket));
thread.start();

When a client connection is accepted, a new Socket instance is created to handle the client request. A new Thread is then started to execute the handleClientRequest method, passing the client socket as an argument. This allows the server to handle multiple client connections concurrently.

Handling Client Request:

InputStream clientIn = clientSocket.getInputStream();
OutputStream clientOut = clientSocket.getOutputStream();

byte[] buffer = new byte[4096];
int bytesRead;

// Read the client's request
bytesRead = clientIn.read(buffer);
String request = new String(buffer, 0, bytesRead);

// Extract the target URL from the request
String targetUrl = extractTargetUrl(request);

// ... (continued below)

These lines set up the input and output streams of the client socket to read the client's request and send the response. A buffer is created to store the data read from the client. The client's request is read from the input stream into the buffer and converted to a string.

Socket targetSocket = new Socket(targetUrl, 80);
InputStream targetIn = targetSocket.getInputStream();
OutputStream targetOut = targetSocket.getOutputStream();

This code establishes a new socket connection with the target server specified by the targetUrl. The port 80 is used for the target server's default HTTP communication.

Forwarding Request and Response:

targetOut.write(request.getBytes());
targetOut.flush();

// Forward the target server's response to the client
while ((bytesRead = targetIn.read(buffer)) != -1) {
    clientOut.write(buffer, 0, bytesRead);
    clientOut.flush();
}

The client's request is forwarded to the target server by writing the request bytes to the output stream of the target socket. The response from the target server is read in a loop from the input stream of the target socket and forwarded back to the client by writing it to the output stream of the client socket.

Closing Connections:

targetSocket.close();
clientSocket.close();

After forwarding the response, the sockets for both the target server and the client are closed to release resources.

Step 4: Run the Proxy Server Save the SimpleProxyServer.java file. In your terminal or command prompt, navigate to the project directory. Compile the Java class by running the following command:

javac SimpleProxyServer.java

Then, run the proxy server using the following command:

java SimpleProxyServer

You should see a message indicating that the server is running at http://localhost:9097.

Step 5: Test the Proxy Server To test the proxy server, open your web browser and configure it to use localhost as the proxy server and 9097 as the port. Then, visit any website using the browser, and you should see the response from the target website displayed.

Congratulations! You have successfully created a simple HTTP proxy server using Java.

Please note that this implementation is a basic example and may not handle certain scenarios or provide the same level of functionality as a full-fledged proxy server.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

With millions of high speed rotating proxies located all over the world,With our automatic IP rotationWith our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)With our automatic CAPTCHA solving technology,

Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

The whole thing can be accessed by a simple API like below in any programming language.

In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so.

curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"

We have a running offer of 1000 API calls completely free. Register and get your free API Key here.

Share this article:

Get our articles in your inbox

Dont miss our best tips/tricks/tutorials about Web Scraping
Only great content, we don’t share your email with third parties.
Icon