SoFunction
Updated on 2025-04-05

How to set up proxy ip to achieve efficient network requests in Java

When developing Java applications, setting up a proxy IP can help improve security and achieve specific network request requirements. Whether in crawlers, API calls or network testing, the use of proxy IP is becoming increasingly important. Today, we will explore how to set up a proxy IP in Java.

1. Set up the proxy using system properties

Java provides an easy way to set up a proxy through system properties. You can set up HTTP and HTTPS proxy using the following code in your program:

("", "Your agentIPaddress");
("", "Proxy port");
("", "Your agentIPaddress");
("", "Proxy port");

For example, if your proxy IP is `192.168.1.100` and the port is `8080`, you can set it like this:

("", "192.168.1.100");
("", "8080");
("", "192.168.1.100");
("", "8080");

2. Set up a proxy in the URL connection

In addition to using system properties, you can also set up a proxy directly when creating `HttpURLConnection`. Here is an example:

import ;
import ;
import ;
import ;
import ;
import ;

public class ProxyExample {
    public static void main(String[] args) {
        try {
            String proxyHost = "192.168.1.100";  // Proxy IP            int proxyPort = 8080;  // Agent port
            // Create a proxy object            Proxy proxy = new Proxy(, new InetSocketAddress(proxyHost, proxyPort));

            // Create a URL connection            URL url = new URL("");
            HttpURLConnection connection = (HttpURLConnection) (proxy);

            // Set the request method            ("GET");

            // Read the response            BufferedReader in = new BufferedReader(new InputStreamReader(()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = ()) != null) {
                (inputLine);
            }
            ();

            // Output response            (());
        } catch (Exception e) {
            ();
        }
    }
}

3. Set up an authentication proxy

If your proxy server requires authentication, you can add basic authentication information to the request. Here is an example of how to set up authentication in `HttpURLConnection`:

import ;
import ;
import ;
import ;
import ;
import ;
import .Base64;

public class AuthProxyExample {
    public static void main(String[] args) {
        try {
            String proxyHost = "192.168.1.100";  // Proxy IP            int proxyPort = 8080;  // Agent port            String username = "yourUsername";  // Agent username            String password = "yourPassword";  // Agent password
            // Create a proxy object            Proxy proxy = new Proxy(, new InetSocketAddress(proxyHost, proxyPort));

            // Create a URL connection            URL url = new URL("");
            HttpURLConnection connection = (HttpURLConnection) (proxy);

            // Set the request method            ("GET");

            // Add authentication            String encoded = ().encodeToString((username + ":" + password).getBytes("UTF-8"));
            ("Proxy-Authorization", "Basic " + encoded);

            // Read the response            BufferedReader in = new BufferedReader(new InputStreamReader(()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = ()) != null) {
                (inputLine);
            }
            ();

            // Output response            (());
        } catch (Exception e) {
            ();
        }
    }
}

4. Use third-party libraries

If you need more complex proxy settings, or want to simplify your code, consider using a third-party library, such as Apache HttpClient. Here is an example of setting up a proxy using Apache HttpClient:

import ;
import ;
import ;
import ;
import ;
import ;

public class ApacheHttpClientProxyExample {
    public static void main(String[] args) {
        try {
            String proxyHost = "192.168.1.100";  // Proxy IP            int proxyPort = 8080;  // Agent port
            // Create a proxy            HttpHost proxy = new HttpHost(proxyHost, proxyPort);

            // Create HttpClient            CloseableHttpClient httpClient = ()
                    .setProxy(proxy)
                    .build();

            // Create a GET request            HttpGet httpGet = new HttpGet("");

            // Execute the request            HttpResponse response = (httpGet);

            // Output response status            ("Response Status: " + ());

            // Close HttpClient            ();
        } catch (Exception e) {
            ();
        }
    }
}

Summarize

Through the above methods, you can easily set up a proxy IP in Java. Whether using system properties, setting up proxy directly in the connection, or using third-party libraries, Java provides flexible ways to meet your needs. Mastering these skills will help you achieve greater flexibility and security in network requests.

This is the article about how to set up proxy IP in Java to achieve efficient network requests. For more related content on setting proxy IP in Java, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!