推荐两个代理IP网站:
1. 全网代理IP:http://proxy.goubanjia.com/
2. 爱赚免费IP:http://ip.izmoney.com/
Java语言有两种方式使用代理IP访问网址并获取内容,
方式一,设置System系统属性
// 设置代理IP
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", "218.26.204.66");
System.getProperties().setProperty("http.proxyPort", "8080");
HttpUrlConnection connection = (HttpUrlConnection)new URL("http://www.baidu.com/").openConnection();
connection.setConnectTimeout(6000); // 6s
connection.setReadTimeout(6000);
connection.setUseCaches(false);
if(connection.getResponseCode == 200){
System.out.println("使用代理IP连接网络成功");
}
方式二,使用java.net.Proxy类
// 使用java.net.Proxy类设置代理IP
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.22.43.3", 8080); HttpUrlConnection connection = (HttpUrlConnection)new URL("http://www.baidu.com/").openConnection(proxy);
connection.setConnectTimeout(6000); // 6s
connection.setReadTimeout(6000);
connection.setUseCaches(false);
if(connection.getResponseCode == 200){
System.out.println("使用代理IP连接网络成功");
}