Jump to Real's How-to Main page

Connect through a Proxy

You have to set the following properties :
http.proxyHost (default: <none>)
http.proxyPort (default: 80 if http.proxyHost specified)
http.nonProxyHosts (default: <none>)
NOTE: proxyHost, proxyPort are deprecated. you have to prefix them with "http.".
NOTE: Those properties are documented here : http://java.sun.com/j2se/1.4/docs/guide/net/properties.html.

You can set the required properties when starting the JVM for a JAVA application from the command line:

java  -Dhttp.proxyhost=myproxyserver.com  -Dhttp.proxyport=80 MyJavaApp
Or in your source :
import java.util.Properties;
...
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "myProxyServer.com");
systemSettings.put("http.proxyPort", "80");
System.setProperties(systemSettings);

You might need to identify yourself to the proxy server.

One way is to use the HTTP property "Proxy-Authorization" with a username:password base64 encoded.

Properties systemSettings = System.getProperties();
...
System.setProperties(systemSettings);

URL url=new URL("http://someserver/somepage");
URLConnection uc = url.openConnection ();
String encoded = new String
      (Base64.base64Encode(new String("username:password").getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
uc.connect();
NOTE: For a base64 function, see this How-to.
The following example dumps the content of a URL but before we identify ourself to the proxy
import java.net.*;
import java.io.*;
import java.util.Properties;

public class URLUtils {
  public static void main(String s[]) {
    URLUtils.dump("http://www.yahoo.com");
    System.out.println("**************");
    URLUtils.dump("https://www.paypal.com");
    System.out.println("**************");
  }

  public static void dump(String URLName){
    try {
      DataInputStream di = null;
      FileOutputStream fo = null;
      byte [] b = new byte[1];

      // PROXY
      Properties systemSettings = System.getProperties();
      systemSettings.put("http.proxyHost","proxy.mydomain.local") ;
      systemSettings.put("http.proxyPort", "80") ;

      URL u = new URL(URLName);
      HttpURLConnection con = (HttpURLConnection) u.openConnection();
      //
      // it's not the greatest idea to use a sun.misc.* class
      // Sun strongly advises not to use them since they can
      // change or go away in a future release so beware.
      //
      sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
      String encodedUserPwd =
         encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
      con.setRequestProperty
         ("Proxy-Authorization", "Basic " + encodedUserPwd);
      // PROXY ----------

      di = new DataInputStream(con.getInputStream());
      while(-1 != di.read(b,0,1)) {
         System.out.print(new String(b));
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
With JDK1.2, the java.net.Authenticator can be used to send the credentials when needed. When no username/password are provided then popup is shown to ask for the credentials.
public static void dump(String URLName){
  try {
    DataInputStream di = null;
    FileOutputStream fo = null;
    byte [] b = new byte[1];
 
    // PROXY
    Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost","proxy.mydomain.local") ;
    systemSettings.put("http.proxyPort", "80") ;
 
    Authenticator.setDefault(new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new
           PasswordAuthentication("mydomain\\username","password".toCharArray());
    }});
 
    URL u = new URL(URLName);
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    di = new DataInputStream(con.getInputStream());
    while(-1 != di.read(b,0,1)) {
       System.out.print(new String(b));
    }
  }
  catch (Exception e) {
          e.printStackTrace();
  }
}

In intranet environment, you may need to bypass the proxy server and go directly to the http server.

The http.nonProxyHosts property indicates the hosts which should be connected too directly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.

java.exe  
   -Dhttp.nonProxyHosts="*.mycompany.com|*.mycompany.local|localhost" 
      MyClass

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2007
[ home ]