SoFunction
Updated on 2025-03-04

Java uses encoding to detect whether the proxy ip is valid

Use Java language method to detect whether the proxy IP is valid based on packages (simple HTTP request detection):

import ;
import ;
import ;
import ;
import ;
 
public class ProxyChecker {
    public static boolean checkProxy(String proxy) {
        try {
            String[] parts = (":");
            String ip = parts[0];
            int port = (parts[1]);
            Proxy proxyObj = new Proxy(, new InetSocketAddress(ip, port));
            URL url = new URL("");
            HttpURLConnection connection = (HttpURLConnection) (proxyObj);
            (5000);
            (5000);
            int responseCode = ();
            if (responseCode == 200) {
                return true;
            } else {
                return false;
            }
        } catch (IOException | NumberFormatException e) {
            return false;
        }
    }

Code analysis: In the checkProxy method, first split the IP address and port number from the proxy IP string.

Creates a Proxy object that specifies the address of the proxy type HTTP and proxy server.

Then try to open a URL connection using the proxy, here is, and set the connection timeout and read timeout to 5 seconds.

Get the response code. If it is 200, it means that the proxy is valid and returns true; otherwise, IOException (network-related exception) and NumberFormatException (port number conversion exception) are captured and false.

The second example: Based on (lower TCP connection detection)

import ;
import ;
 
public class ProxySocketChecker {
    public static boolean checkProxySocket(String proxy) {
        try {
            String[] parts = (":");
            String ip = parts[0];
            int port = (parts[1]);
            Socket socket = new Socket();
            (new (ip, port), 5000);
            ();
            return true;
        } catch (IOException | NumberFormatException e) {
            return false;
        }
    }
}

Code explanation: In the checkProxySocket method, the proxy IP string is also split first to get the IP and port number.

Create a Socket object, try to connect to the proxy server, and set the timeout to 5 seconds.

If the connection is successful, close the Socket and return true; otherwise, catch the exception and return false.

These methods are just basic detection methods. In actual applications, more complex detection can be carried out based on specific needs (such as whether the detection agent supports a specific protocol, whether it can hide the real IP, etc.). At the same time, when conducting a large number of proxy IP detections, you should pay attention to complying with the website usage rules to avoid excessively frequent requests and causing bans.

This is the article about whether Java uses encoding to detect whether proxy IP is valid. For more related Java detection proxy IP content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!