
First, proxy IP in Java in the end can do what?
For example, you wrote a crawler program to grab data, but the target website blocked your IP address, so you need a proxy IP address.I'll get you a new vest and get back to work.In Java, setting up a proxy is actually very simple, just like giving a courier boy a different delivery route, mainly in two positions:
// Method 1: System global setting
System.setProperty("http.proxyHost", "121.40.62.18");
System.setProperty("http.proxyPort", "9020");
// Method 2: Individual request setup
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("121.40.62.18", 9020));
URL url = new URL("http://目标网站");
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
Second, the actual code configuration skills
Now let's use ipipgo's proxy service as a real example. Let's say you bought theirDynamic Residential Package, get the API address is https://api.ipipgo.com/getproxy, the return format is IP:port:username:password
// Example of using HttpURLConnection
String proxyStr = "121.40.62.18:9020:user123:pass456"; // actually get it from the API
String[] parts = proxyStr.split(":"); // actually get it from the API.
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(parts[2], parts[3].toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); }); }; }; }
URL url = new URL("http://目标网站");
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestMethod("GET");
Third-party libraries for advanced play
If you use a third-party library like OkHttp, the configuration is more flexible. Here's akey reminder: Remember to deal with SSL certificate issues, especially for HTTPS requests:
// OkHttp configuration example
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("121.40.62.18", 9020)))
.proxyAuthenticator((route, response) -> {
String credential = Credentials.basic("user123", "pass456");
return response.request().newBuilder()
.header("Proxy-Authorization", credentials)
.build(); })
})
.sslSocketFactory(sslSocketFactory, trustManager) // Here to handle SSL
.build(); }) .sslSocketFactory(sslSocketFactory, trustManager)
Four, dynamic agent switching black technology
If you're doing data collection.Fixed IP easily blocked. Dynamic switching with ipipgo's API:
// Change the IP every 10 minutes
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
String newProxy = getNewProxyFromAPI(); // Call ipipgo's API to get new IPs
updateProxyConfig(newProxy);
}, 0, 10, TimeUnit.MINUTES);
v. guide to demining common problems
Q: What can I do if the agent can't connect?
A: First check the whitelist settings, ipipgo need to bind the use of IP. then try to test the proxy connectivity directly with the curl command.
Q: Return 407 authentication error?
A: Ninety percent probability that the account password is wrong, pay attention to case sensitive. It is recommended to use Postman to test the authentication information first
Q: HTTPS request failed?
A: SSL contexts need to be handled separately, don't forget to import the certificate chain
VI. Why ipipgo?
I'm sure we should brag about our own products, but we don't brag, we just tell the truth:
| Package Type | Applicable Scenarios | Price advantage |
|---|---|---|
| Dynamic residential (standard) | Small and medium-sized crawlers | 7.67 Yuan/GB |
| Dynamic Residential (Business) | Large-scale data acquisition | 9.47 Yuan/GB |
| Static homes | Long-term fixed operations | 35RMB/IP |
Special mention of theirTK LineThe actual delay is lower than the ordinary line 40%, but we will not put the specific data so as not to be like an advertisement. If you need to find customer service to test IP.
One final rant: don't buy an agent based on price alone.IP purityThat's the key. Some of the cheap packages use the IP of the server room, which is recognized in minutes. ipipgo's residential proxies are real home broadband, which is especially obvious in the Header checksum, and the values of these fields of X-Forwarded-For look real.

