驗證代理是否可以使用
package src;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class TestProxy {
public boolean doTest(String proxyIP, int port) throws Exception {
URL url = new URL("http://www.g.cn/");
//URL url = new URL("http://www.g.cn/");
SocketAddress addr = new InetSocketAddress(proxyIP, port);
Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);
URLConnection conn = url.openConnection(typeProxy);
if (conn == null)
return false;

conn.setConnectTimeout(1000);
InputStream in = conn.getInputStream();
byte[] b = new byte[1024];
while (in.read(b) > 0) {
System.out.print(new String(b));
}
System.out.println();
return true;

}

public String[] getProxyIp() throws Exception {
FileReader fr = new FileReader("c://ip.txt");
BufferedReader br = new BufferedReader(fr);
String line;
List<String> rt = new ArrayList<String>();
String ip, port;
while ((line = br.readLine()) != null) {
rt.add(line.trim());
}
fr.close();
String[] aa = new String[rt.size()];
rt.toArray(aa);
return aa;
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
TestProxy test = new TestProxy();
String[] items;
try {
items = test.getProxyIp();
for (String proxy : items) {
try {
StringTokenizer st = new StringTokenizer(proxy, ":");
String ip = st.nextToken();
String port = st.nextToken();
if (test.doTest(ip, Integer.parseInt(port))) {
System.err.println(proxy);
} else {
System.out.println(proxy);
}
} catch (Exception ex) {
// ex.printStackTrace();
System.out.println(proxy + ":" + ex.toString());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
ip.txt
125.245.196.194:8080
203.110.240.22:80
77.240.82.6:80
71.5.68.4:3128
213.41.71.164:80
203.160.1.103:80
200.174.85.195:3128
60.28.194.172:80
148.233.159.58:8080
207.248.228.166:80
203.162.183.222:80
60.218.99.18:8080
203.160.1.112:80
193.93.112.33:8080
222.134.58.246:3128
121.57.4.17:8080
221.214.220.228:3128
20.132.16.22:80
121.12.249.207:3128
219.80.28.24:80
200.174.85.193:3128




















































































ip.txt
125.245.196.194:8080
203.110.240.22:80
77.240.82.6:80
71.5.68.4:3128
213.41.71.164:80
203.160.1.103:80
200.174.85.195:3128
60.28.194.172:80
148.233.159.58:8080
207.248.228.166:80
203.162.183.222:80
60.218.99.18:8080
203.160.1.112:80
193.93.112.33:8080
222.134.58.246:3128
121.57.4.17:8080
221.214.220.228:3128
20.132.16.22:80
121.12.249.207:3128
219.80.28.24:80
200.174.85.193:3128