Connect through a Proxy
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import sun.misc.BASE64Encoder;
public class Main {
public static void main(String[] argv) throws Exception {
byte[] b = new byte[1];
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "proxy.mydomain.local");
systemSettings.put("http.proxyPort", "80");
URL u = new URL("http://www.google.com");
HttpURLConnection con = (HttpURLConnection) u.openConnection();
BASE64Encoder encoder = new BASE64Encoder();
String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
DataInputStream di = new DataInputStream(con.getInputStream());
while (-1 != di.read(b, 0, 1)) {
System.out.print(new String(b));
}
}
}
Related examples in the same category