Skip to main content
Java

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

Friedrich Kräft avatar
Written by Friedrich Kräft
Updated over a year ago

Java Code

In this example, we use the Residential proxy below:

resi.ipv6.plainproxies.com:8080:customer:product--resi6--pass--nicepass

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 = "resi.ipv6.plainproxies.com:8080:customer:product--resi6--pass--nicepass";
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()));
}
}

Did this answer your question?