Java 验证代理ip

 原文地址:http://www.cnblogs.com/junrong624/p/5416503.html

 1 import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**<br> * 批量代理IP有效检测<br> *<br> * @param IP<br> * @param post<br> */
public static void createIPAddress(String ip,int port) {
URL url = null;
try {
url = new URL("http://www.baidu.com");
} catch (MalformedURLException e) {
System.out.println("url invalidate");
}
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();
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("baidu") > ) {//有效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(); }
 /**
* 批量代理IP有效检测
*
* @param proxyIpMap
* @param reqUrl
*/
public static void checkProxyIp(Map<String, Integer> proxyIpMap, String reqUrl) { for (String proxyHost : proxyIpMap.keySet()) {
Integer proxyPort = proxyIpMap.get(proxyHost); int statusCode = ;
try {
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); // 连接超时时间(默认10秒 10000ms) 单位毫秒(ms)
int connectionTimeout = ;
// 读取数据超时时间(默认30秒 30000ms) 单位毫秒(ms)
int soTimeout = ;
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); HttpMethod method = new GetMethod(reqUrl); statusCode = httpClient.executeMethod(method);
} catch (Exception e) {
System.out.println("ip " + proxyHost + " is not aviable");
}
if(statusCode>){
System.out.format("%s:%s-->%sn", proxyHost, proxyPort,statusCode);
} }
}
上一篇:CString——Left、Right、Find、ReverseFind


下一篇:oracle 判断字符串是否包含指定内容