Click here to Skip to main content
16,004,540 members
Articles / Web Development
Tip/Trick

Making HTTP Requests Through a Proxy with Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Aug 2024CPOL1 min read 2.2K   1  
A quick code solution of how to make HTTP requests through a proxy with Java
This Java code example demonstrates how to make HTTP requests through a proxy server with basic authentication. By configuring the HttpURLConnection with a proxy and adding the necessary Proxy-Authorization header, this snippet allows you to route requests through a specified proxy server while handling authentication. The example connects to a URL, processes the response, and prints the result, showcasing how to integrate proxy settings into your Java networking code effectively.

Introduction

In the realm of network programming, making HTTP requests through a proxy server can be essential for various reasons, including web scraping, managing multiple requests, or boosting anonymity. This article presents a Java code snippet that demonstrates how to configure an HttpURLConnection to route requests through a proxy server with basic authentication.

Using the code

To use this Java code for making HTTP requests through a proxy with basic authentication:

  1. Update Configuration:

    • Set proxyHost, proxyPort, username, and password with your proxy server details.
    • Modify the url variable to the target endpoint.
  2. Compile and Run:

    • Compile the code using javac Main.java.
    • Run the compiled class with java Main.
  3. Key Components:

    • Proxy Setup: Configured via Proxy and InetSocketAddress.
    • Authentication: Added with Proxy-Authorization header using Base64 encoding.
    • Response Handling: Read and print the response with BufferedReader.
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws Exception {
        try {
            String proxyHost = "us.swiftproxy.net";
            int proxyPort = 7878;
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            String username = "username_custom_zone_us";
            String password = "password";
            String credentials = username + ":" + password;
            URL url = new URL("http://ipinfo.io");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes()));
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println((response.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Conclusion

This Java code snippet provides a straightforward method for making HTTP requests through a proxy server with basic authentication. By configuring the HttpURLConnection with the appropriate proxy settings and authentication headers, you can manage network requests effectively while maintaining control over your IP address and access credentials. Whether you’re dealing with IP restrictions, conducting research, or managing multiple accounts, this approach offers a practical solution. For enhanced security and functionality, remember to handle sensitive information carefully and tailor the proxy settings to your specific needs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --