After purchasing static IP services from IPNut platform, use the following Java code samples for integration.
1. SOCKS5 Proxy Implementation Socks5ProxyDemo.java #
import java.io.*;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Socks5ProxyDemo {
// Proxy configuration (replace with your credentials)
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== IPNut SOCKS5 Proxy Demo ===\n");
socks5WithHttpClient();
socks5WithSocket();
socks5WithCustomRequest();
}
/**
* SOCKS5 Proxy using Java 11+ HttpClient
*/
public static void socks5WithHttpClient() {
System.out.println("1. SOCKS5 Proxy with HttpClient:");
try {
// Create SOCKS5 proxy selector
ProxySelector proxySelector = new ProxySelector() {
@Override
public java.util.List select(URI uri) {
return java.util.List.of(
new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(PROXY_HOST, PROXY_PORT))
);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
System.err.println("Connection failed: " + uri + " - " + ioe.getMessage());
}
};
// Create HttpClient with SOCKS5 proxy
HttpClient client = HttpClient.newBuilder()
.proxy(proxySelector)
.connectTimeout(Duration.ofSeconds(30))
.version(HttpClient.Version.HTTP_1_1)
.build();
// Create HTTP request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.header("User-Agent", "IPNut-Java-Client/1.0")
.GET()
.build();
// Execute request
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response: " + response.body());
System.out.println("✅ SOCKS5 Proxy test completed successfully");
} catch (Exception e) {
System.err.println("❌ Request failed: " + e.getMessage());
}
}
/**
* SOCKS5 Proxy using raw Socket connection
*/
public static void socks5WithSocket() {
System.out.println("\n2. SOCKS5 Proxy with Socket:");
Socket socket = null;
try {
// Create proxy configuration
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(PROXY_HOST, PROXY_PORT));
// Create socket connection through proxy
socket = new Socket(proxy);
socket.connect(new InetSocketAddress("httpbin.org", 80), 30000);
// Send HTTP request
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// Build HTTP request
String request = "GET /ip HTTP/1.1\r\n" +
"Host: httpbin.org\r\n" +
"User-Agent: IPNut-SOCKS5-Socket-Client\r\n" +
"Accept: application/json\r\n" +
"Connection: close\r\n\r\n";
out.print(request);
out.flush();
// Read response
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line).append("\n");
}
System.out.println("Response received:");
System.out.println(response.toString());
System.out.println("✅ SOCKS5 Socket test completed successfully");
} catch (Exception e) {
System.err.println("❌ Socket request failed: " + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.err.println("Socket close error: " + e.getMessage());
}
}
}
}
/**
* SOCKS5 Proxy with custom configuration
*/
public static void socks5WithCustomRequest() {
System.out.println("\n3. SOCKS5 Proxy with Custom Request:");
try {
// Set SOCKS5 proxy system properties
System.setProperty("socksProxyHost", PROXY_HOST);
System.setProperty("socksProxyPort", String.valueOf(PROXY_PORT));
// Create HttpClient
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
// Create custom request with headers
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/headers"))
.header("User-Agent", "IPNut-Custom-Client/1.0")
.header("Accept", "application/json")
.header("X-Proxy-Type", "SOCKS5")
.header("X-Client-ID", "IPNut-Java-Demo")
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Custom Headers Response: " + response.body());
System.out.println("✅ Custom SOCKS5 request test completed");
} catch (Exception e) {
System.err.println("❌ Custom request failed: " + e.getMessage());
} finally {
// Clean up system properties
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
}
}
}
2. HTTP Proxy Implementation HttpProxyDemo.java #
import java.io.*;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Base64;
public class HttpProxyDemo {
// Proxy configuration
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== IPNut HTTP Proxy Demo ===\n");
httpProxyWithHttpClient();
httpProxyWithAuthenticator();
httpProxyWithSocket();
httpProxyMultipleRequests();
}
/**
* HTTP Proxy using HttpClient with proxy selector
*/
public static void httpProxyWithHttpClient() {
System.out.println("1. HTTP Proxy with HttpClient:");
try {
// Create HTTP proxy selector
ProxySelector proxySelector = new ProxySelector() {
@Override
public java.util.List select(URI uri) {
return java.util.List.of(
new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(PROXY_HOST, PROXY_PORT))
);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
System.err.println("Proxy connection failed: " + ioe.getMessage());
}
};
HttpClient client = HttpClient.newBuilder()
.proxy(proxySelector)
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.header("User-Agent", "IPNut-HTTP-Client/1.0")
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response: " + response.body());
System.out.println("✅ HTTP Proxy test completed successfully");
} catch (Exception e) {
System.err.println("❌ HTTP Proxy request failed: " + e.getMessage());
}
}
/**
* HTTP Proxy with authentication using Authenticator
*/
public static void httpProxyWithAuthenticator() {
System.out.println("\n2. HTTP Proxy with Authentication:");
try {
// Set up proxy authentication
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == Authenticator.RequestorType.PROXY) {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
return null;
}
});
// Configure system proxy settings
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", String.valueOf(PROXY_PORT));
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.header("User-Agent", "IPNut-Auth-Client/1.0")
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response: " + response.body());
System.out.println("✅ Authenticated HTTP Proxy test completed");
} catch (Exception e) {
System.err.println("❌ Authenticated request failed: " + e.getMessage());
} finally {
// Clean up
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
}
}
/**
* HTTP Proxy using raw Socket with manual authentication
*/
public static void httpProxyWithSocket() {
System.out.println("\n3. HTTP Proxy with Socket (Manual Auth):");
Socket socket = null;
try {
// Connect to proxy server
socket = new Socket(PROXY_HOST, PROXY_PORT);
// Build proxy authentication
String credentials = PROXY_USERNAME + ":" + PROXY_PASSWORD;
String encodedCredentials = Base64.getEncoder()
.encodeToString(credentials.getBytes());
// Send CONNECT request to proxy
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String connectRequest = "CONNECT httpbin.org:443 HTTP/1.1\r\n" +
"Host: httpbin.org:443\r\n" +
"Proxy-Authorization: Basic " + encodedCredentials + "\r\n" +
"Connection: keep-alive\r\n\r\n";
out.print(connectRequest);
out.flush();
// Read proxy response
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty()) break;
if (line.contains("200")) {
System.out.println("✅ Proxy connection established");
}
}
// For HTTPS, you would need to handle SSL handshake
// This example shows the CONNECT method for tunneling
System.out.println("✅ HTTP Proxy Socket test completed");
} catch (Exception e) {
System.err.println("❌ Socket proxy request failed: " + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.err.println("Socket close error: " + e.getMessage());
}
}
}
}
/**
* Multiple requests through HTTP Proxy
*/
public static void httpProxyMultipleRequests() {
System.out.println("\n4. Multiple Requests through HTTP Proxy:");
try {
// Configure proxy
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
// Set up authentication
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
});
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
String[] testEndpoints = {
"https://httpbin.org/ip",
"https://httpbin.org/user-agent",
"https://httpbin.org/headers",
"https://httpbin.org/get?test=ipnut_proxy"
};
for (int i = 0; i < testEndpoints.length; i++) {
System.out.println("\nRequest " + (i + 1) + ": " + testEndpoints[i]);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(testEndpoints[i]))
.header("User-Agent", "IPNut-Multi-Request-Client/1.0")
.header("X-Request-ID", "test-" + (i + 1))
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response Preview: " +
response.body().substring(0, Math.min(150, response.body().length())) + "...");
// Rate limiting
Thread.sleep(1000);
}
System.out.println("\n✅ All multiple requests completed successfully");
} catch (Exception e) {
System.err.println("❌ Multiple requests failed: " + e.getMessage());
} finally {
// Clean up
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
}
3. Connectivity Testing Utility ProxyTestTool.java #
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ProxyTestTool {
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== IPNut Proxy Connectivity Test ===\n");
testSocks5Proxy();
testHttpProxy();
comprehensiveTest();
}
/**
* Test SOCKS5 Proxy connectivity
*/
public static void testSocks5Proxy() {
System.out.println("Testing SOCKS5 Proxy Connectivity:");
try {
// Configure SOCKS5 proxy
System.setProperty("socksProxyHost", PROXY_HOST);
System.setProperty("socksProxyPort", String.valueOf(PROXY_PORT));
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.timeout(Duration.ofSeconds(15))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("✅ SOCKS5 Proxy Connection Successful");
System.out.println(" Status Code: " + response.statusCode());
System.out.println(" Assigned IP: " + response.body());
} catch (Exception e) {
System.err.println("❌ SOCKS5 Proxy Connection Failed: " + e.getMessage());
} finally {
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
}
}
/**
* Test HTTP Proxy connectivity
*/
public static void testHttpProxy() {
System.out.println("\nTesting HTTP Proxy Connectivity:");
try {
// Configure HTTP proxy
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
// Set up authentication
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
});
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.timeout(Duration.ofSeconds(15))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("✅ HTTP Proxy Connection Successful");
System.out.println(" Status Code: " + response.statusCode());
System.out.println(" Assigned IP: " + response.body());
} catch (Exception e) {
System.err.println("❌ HTTP Proxy Connection Failed: " + e.getMessage());
} finally {
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
/**
* Comprehensive proxy testing
*/
public static void comprehensiveTest() {
System.out.println("\n=== Comprehensive Proxy Test ===");
String[] testUrls = {
"https://httpbin.org/ip",
"https://httpbin.org/user-agent",
"https://httpbin.org/headers"
};
// Test both proxy types
String[] proxyTypes = {"SOCKS5", "HTTP"};
for (String proxyType : proxyTypes) {
System.out.println("\nTesting " + proxyType + " Proxy:");
for (String testUrl : testUrls) {
try {
boolean success = testProxyConnection(proxyType, testUrl);
if (success) {
System.out.println(" ✅ " + testUrl + " - Success");
} else {
System.out.println(" ❌ " + testUrl + " - Failed");
}
} catch (Exception e) {
System.out.println(" ❌ " + testUrl + " - Error: " + e.getMessage());
}
}
}
}
private static boolean testProxyConnection(String proxyType, String testUrl) {
try {
if ("SOCKS5".equals(proxyType)) {
System.setProperty("socksProxyHost", PROXY_HOST);
System.setProperty("socksProxyPort", String.valueOf(PROXY_PORT));
} else {
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
});
}
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(testUrl))
.timeout(Duration.ofSeconds(10))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200;
} catch (Exception e) {
return false;
} finally {
// Clean up
if ("SOCKS5".equals(proxyType)) {
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
} else {
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
}
}
4. Compilation and Execution #
# Compilation Commands:
# Compile all Java files
javac *.java
# Or compile individually
javac Socks5ProxyDemo.java
javac HttpProxyDemo.java
javac ProxyTestTool.java
# Execution Commands:
# Run SOCKS5 proxy demo
java Socks5ProxyDemo
# Run HTTP proxy demo
java HttpProxyDemo
# Run connectivity tests
java ProxyTestTool
5. Project Structure & Dependencies #
# Maven Dependencies (pom.xml):
11
11
# Gradle Dependencies (build.gradle):
plugins {
id 'java'
}
sourceCompatibility = 11
targetCompatibility = 11
Key Integration Notes:
Java Version: Requires Java 11 or higher for HTTP Client
Credential Management: Replace example credentials with actual IPNut proxy details
Error Handling: Comprehensive exception handling for production use
Performance: Connection timeouts and proper resource management
Security: Clean up system properties after use
Best Practices:
Resource Management: Always close sockets and streams in finally blocks
Timeout Configuration: Set appropriate timeouts for different operations
Error Recovery: Implement retry mechanisms for transient failures
Monitoring: Add logging for connection status and performance metrics
Technical Support
For integration assistance or location-specific requirements:
Email: Support@ipnut.com
Live Chat: 24/7 real-time support via official website
*All code examples support both HTTP and SOCKS5 protocols with enterprise-grade authentication and security features.*