Here are 2 ways I can do it: the first is createipaddress () and convertstreamtostring ()
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;/**
* Bulk Agent IP Effective detection
*
* @param IP
* @param post
*/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 Proxyinputstream in = null;try {URLConnection conn = url.openconnection (proxy); Conn.setconnecttimeout (+); in = Conn.getinputstream ();} catch (Exception e) {System.out.println ("IP" + IP + "is not aviable");//exception ip}string s = convertstreamtostring (in); System.out.println (s);//System.out.println (s); if (S.indexof ("Baidu") > 0) {//valid IPSYSTEM.OUT.PRINTLN (IP + ":" +port + "is OK");}} public static String convertstreamtostring (InputStream was) {if (is = = null) return ""; BufferedReader reader = new BufferedReader (new InputStreamReader (IS)); StringBuilder sb = new StringBuilder (); String line = null;try {when (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 ();}
The second method is Checkproxyip ()
/** * Bulk Agent IP Valid detection * * @param proxyipmap * @param requrl * * public static void Checkproxyip (map<st Ring, integer> Proxyipmap, String requrl) {for (String ProxyHost:proxyIpMap.keySet ()) {Inte Ger ProxyPort = Proxyipmap.get (proxyhost); int statusCode = 0; try {HttpClient HttpClient = new HttpClient (); Httpclient.gethostconfiguration (). SetProxy (ProxyHost, ProxyPort); Connection time-out (default 10 seconds 10000ms) unit milliseconds (ms) int connectiontimeout = 10000; Read data time-out (default 30 seconds 30000ms) unit milliseconds (ms) int sotimeout = 30000; 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>0) {System.out.format ("%s:%s-->%sn", ProxyHost, Proxyport,statuscode) ; } } }
The Java Detection Agent IP is accurate