环境:windows+mysql+Myeclipse
1:找几个网址专门分享代理的,这个自己搜索
2:创建ip代理验证机制:
public static void createIPAddress(URL url,String ip,int port) {
InetSocketAddress addr = null;
addr = new InetSocketAddress(ip, port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy
InputStream in = null;
try {
URLConnection conn = url.openConnection(proxy);
conn.setConnectTimeout(1000);
in = conn.getInputStream();
} catch (Exception e) {
System.out.println("ip " + ip + " is not aviable");//异常IP
}
String s = convertStreamToString(in);
System.out.println(s);
// System.out.println(s);
if (s.indexOf("bai") > 0) {//有效IP
System.out.println(ip + ":"+port+ " is ok");
}
}
public static String convertStreamToString(InputStream is) {
if (is == null)
return "";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
这样可以在main函数验证该ip能不能使用。谢谢