Identify ourself to a proxy : Proxy « Network Protocol « Java






Identify ourself to a proxy

 

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

public class Main{

  public static void main(String s[]) throws Exception{
    try {
      Properties systemSettings = System.getProperties();
      systemSettings.put("proxySet", "true");
      systemSettings.put("http.proxyHost", "proxy.mycompany.local");
      systemSettings.put("http.proxyPort", "80");

      URL u = new URL("http://www.java.com");
      HttpURLConnection con = (HttpURLConnection) u.openConnection();
      sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
      String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
      con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
      con.setRequestMethod("HEAD");
      System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
      System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(false);
    }
  }
}

   
  








Related examples in the same category

1.URL connection and proxy
2.Detect Proxy Settings for Internet Connection