URL connection and proxy : Proxy « Network Protocol « Java






URL connection and 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.Identify ourself to a proxy
2.Detect Proxy Settings for Internet Connection