The Java Detection Agent IP is accurate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.