方式一、设置系统代理
System.setProperty("http.proxyHost", "192.168.254.254");
System.setProperty("http.proxyPort", "9000";
System.setProperty("http.nonProxyHosts", "java.oreilly.com|xml.oreilly.com"); //这两个主机不要代理
方式二、代理类java.net.Proxy
Proxy中的三个枚举对象
Proxy.Type.DIRECT
Proxy.Type.HTTP
Proxy.Type.SOCKS
对于一个代理服务器来说最重要的就是IP和port,下面的代码制作了一个Http代理
SocketAddress address = new InetSocketAddress("proxy.example.com", 80);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
方式三、java 1.5 中新增加了一个类 ProxySelector 具体用法,用到时请查Java Network programming chapter7 7.4
完整实例:
无需帐号密码的代理:
//设置代理
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "10.1.2.188");
System.setProperty("http.proxyPort", "80");
//直接访问目的地址
URL url = new URL("http://www.baidu.com");
URLConnection con = url.openConnection();
InputStreamReader isr = new InputStreamReader(con.getInputStream());
char[] cs = new char[1024];
int i = 0;
while ((i = isr.read(cs)) > 0) {
System.out.println(new String(cs, 0, i));
}
isr.close();
最后总结一下:
在使用HTTP代理的环境中,
如果使用Socket(TCP)连接外网,则直接连接代理服务器,然后在发送的HTTP请求中指明要转发到的外网网址。
如果使用URL(HTTP)连接外网,则需要设置HTTP代理参数或使用Proxy。
OK,明白以后可以随意使用了,看以下代码,使用NIO的Socket通过HTTP代理访问外网的例子:
SocketChannel sc = SocketChannel.open(new InetSocketAddress("10.1.2.188", 80));
sc.write(Charset.forName("utf8").encode("GET http://www.baidu.com/ HTTP/1.1\r\n\r\n"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (sc.read(buffer) != -1) {
buffer.flip();
System.out.println(Charset.forName("utf8").decode(buffer));
buffer.clear();
}
sc.close();
需要帐号密码的代理:
//有些时候我们的网络不能直接连接到外网, 需要使用http或是https或是socket代理来连接到外网, 这里是java使用代理连接到外网的一些方法,:方法一使用系统属性来完成///代理设置, 这种方法比较简单, 但是不能对单独的连接来设置代理:
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() {
returnnew PasswordAuthentication(user, password.toCharArray());
}
}
//方法二使用Proxy来对每个连接实现代理, 这种方法只能在jdk 1.5以上的版本使用(包含jdk1.5), 优点是可以单独的设置每个连接的代理, 缺点是设置比较麻烦:
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();
}
}
取消代理服务器设置
System.getProperties().remove("http.proxyHost");
System.getProperties().remove("http.proxyPort");
System.getProperties().remove("https.proxyHost");
System.getProperties().remove("https.proxyPort");