Search…
⌃K

Java

This article provides an example of using a proxy to make an HTTP request with the Java programming language.

Java Code

In this example, we use the Residential proxy below:
residential.pingproxies.com:7777:customer-tt_pp_lz_5051-sessid-Z9tCGVYHu:23n22mk22
This proxy is split into four parts separated by colons. We first save the proxy as a string variable, then split it into four parts at each colon (:) and then add it, with authentication, to our proxy dictionary.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Application {
private static String proxy_string = "residential.pingproxies.com:7777:customer-tt_pp_lz_5051-sessid-Z9tCGVYHu:23n22mk22";
private static String[] proxy_parts = proxy_string.split(":");
private static String ip_address = proxy_parts[0];
private static int port = Integer.parseInt(proxy_parts[1]);
private static String USERNAME = proxy_parts[2];
private static String PASSWORD = proxy_parts[3];
private static String URL_TO_GET = "http://www.google.com/";
public static void main(String[] args) throws IOException {
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip_address, port));
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
USERNAME, PASSWORD.toCharArray());
}
});
final URL url = new URL(URL_TO_GET);
final URLConnection urlConnection = url.openConnection(proxy);
final BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
final StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
bufferedReader.close();
System.out.println(String.format("Response:\n%s", response.toString()));
}
}