Passing a Proxy Server in Java

Setup the proxyserver
To specify your proxyserver / firewall in java you need to set 2 system variables:

System.setProperty(“http.proxyHost”, proxyhost);
System.setProperty(“http.proxyPort”, proxyport);

where proxyhost is the url (or ip-address) to your proxyserver and proxyport is the port of the proxyserver. (both these variables are passed as java.lang.String)
Once these properties are set, the JVM is aware of your proxyserver and will send all your attempts for connecting to an external IP-Address through this proxy.
If you need to connect without a proxy somewhere else in your code (for ex. to connect to a computer in your LAN), simply remove the proxy settings using this code :

System.getProperties().remove((“http.proxyHost”);
System.getProperties().remove((“http.proxyPort”);

Authenticate with the proxyserver
This is a bit more difficult. Follow these three steps :
If you need to authenticate with your firewall, when sending HTTP-requests, the proxy-authentication parameters need to be set as a HTTP-header parameter. The header parameter is : Proxy-Authorization

Also, the value of this parameter must be of the form : “Basic ” + login + “:” + password
This value must be BASE64 encoded
In the example code, I used a base64 encoder I found on http://java.sun.com in the Java Commerce(TM) Toolkit.

String s = proxylogin + “:” + proxypass;
String se = new BASE64Encoder().encodeBuffer(s.getBytes());

So, the ‘se’ variable now contains the value that can be used to set as http request variable. Setting this variable is done using the .setRequestProperty(String key, String value) on a java.net.HttpURLConnection object.

Check out the full source code :

Source code for Proxy Authentication/Authorization

package net.javacoding.article.proxy;

import javax.commerce.util.BASE64Encoder;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/*
* Author : Tim Goffings (tim@javacoding.net)
* Date: Sep 10, 2002 – 12:30:49 PM
*/
public class PassProxy {

public PassProxy(String proxyhost, String proxyport, String proxylogin, String proxypass, String url) {

/* Set the proxy server host and port */
System.setProperty(“http.proxyHost”, proxyhost);
System.setProperty(“http.proxyPort”, proxyport);

/* Set up and encode the proxy credentials */
String s = proxylogin + “:” + proxypass;
String se = new BASE64Encoder().encodeBuffer(s.getBytes());

try {
/* Create a HttpURLConnection Object and set the properties */
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty(“Proxy-Authorization”, “Basic ” + se);
uc.connect();

/* Print the content of the url to the console. */
showContent(uc);
} catch (IOException e) {
e.printStackTrace();
}
}

private void showContent(HttpURLConnection uc) throws IOException {
InputStream i = uc.getInputStream();
char c;
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}

public static void main(String[] args) {
if (args.length == 5) {
String proxyhost = args[0];
String proxyport = args[1];
String proxylogin = args[2];
String proxypass = args[3];
String url = args[4];
new PassProxy(proxyhost, proxyport, proxylogin, proxypass, url);
} else {
System.out.println(“Usage :\n\t java -jar PassProxy.jar proxyhost proxyport proxylogin proxypass url”);
}
}

}

by Tim Goffings