public static void main(String[] args) {
Properties prop = System.getProperties();
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "192.168.0.254");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "8080");
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
// socks代理服务器的地址与端口
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("userName", "Password"));
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
// 创建代理服务器
InetSocketAddress addr = new InetSocketAddress("192.168.0.254",
8080);
// Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
// 如果我们知道代理server的名字, 可以直接使用
// 结束
URLConnection conn = url.openConnection(proxy);
InputStream in = conn.getInputStream();
// InputStream in = url.openStream();
String s = IOUtils.toString(in);
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/mebusw/archive/2011/04/29/6372714.aspx