Use multithreading, curl, and proxy IP to simulate post submission and get access in Java

Source: Internet
Author: User
Tags finally block throwable

Use multithreading, curl, and proxy IP to simulate post submission and get access in Java

Rookie, multi-threaded fun on the writing to play, the great God can pass advice, brother in this teachable, thank you!


/** * @ Component Name: Javademo * @ Package Name: Javademo * @ file name: Jenny.java * @ created: August 1, 2014 5:53:48 * @ Copyright information: copyright©2014 eelly Co.Ltd, small Aunt son all rights reserved. */package javademo;import java.io.bufferedreader;import Java.io.file;import java.io.filewriter;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.io.Writer;import Java.net.url;import java.net.urlconnection;import java.util.list;import Java.util.map;import java.util.Random;/** * Used for exception throwing but defined because Exception cannot be thrown, only subclasses can */class MyException extends Exception {private static final long Serialversionuid =    -1485825968429580934l;    Public MyException () {super ();    } public MyException (String msg) {super (MSG);    } public MyException (String msg, throwable cause) {super (MSG, cause);    } public MyException (Throwable cause) {super (cause); }}/** * A sister-in-Need thread class */class Shimengren implements Runnable {/** * pageviews link */private String URL = "Http://ww W.shimengren.com/artIcle/yj/views.jhtml ";    /** * Likes the link */private String Urlzan = "http://www.shimengren.com/article/yj/ups.jhtml";    /** * Travel's ID */private String param = "article_id=10844";    /** * Multi-threaded Shared global variable *//private volatile Boolean flag = true;    /** * The number of threads that enter the post submission at the same time */private static final int max_thread_count = 25;    /** * The counter of the number of threads that enter the post submission at the same time */private volatile int threadnum = 0;    /** * * Count */private static int num = 0;    /** * Browse times */private static int view = 0;            /** * Proxy IP */private string[] ProxyArray = {"127.0.0.1:8787", "115.239.210.199:80", "149.255.255.242:80", "124.172.242.175:80", "117.59.217.243:80", "183.234.59.89:18186", "117.59.217.236:80", "183.224.1.56:80  "," 120.202.249.230:80 "," 125.46.100.198:9999 "," 112.65.19.122:8080 "," 202.96.172.234:808 "," 183.224.1.114:80 ",    "183.224.1.113:80", "222.66.115.229:80"}; public void Setflag (Boolean Flag) {//This.flag = flag;    }///public Boolean Getflag () {//return this.flag;    }///public void Stop () {//This.setflag (FALSE);    }///public void Start () {//This.setflag (TRUE);    } public void setnum (int num) {shimengren.num = num;    } public int Getnum () {return shimengren.num;    } public void Setthreadnum (int threadnum) {shimengren.threadnum = Threadnum;    } public int Getthreadnum () {return shimengren.threadnum;    } public int GetView () {return shimengren.view;     }/** * @ Method Name: GetProxy * Description: Random get proxy IP * @ Creator: <a Href=mailto: [email protected]> sister-in-law </a> * @ Modified By: * @ Modified: August 18, 2014 9:23:36 * @return * @ return value: string[] * @ Exception Description: */public string[] Getpr        Oxy () {string[] strlist = null;        int len = proxyarray.length-1;        int num = 0; if (0 < len) {num = new Random (). NEXTInt (len);        } String proxy = This.proxyarray[num];        if (Proxy! = "") {strlist = Proxy.split (":");    } return strlist; }/** * @ Method Name: Sendget * Description: A request to send a GET method to a specified URL * @ Creator: <a Href=mailto: [email protected]> sister-in-law </            A> * @ Modified By: * @ Modified: August 18, 2014 morning 9:26:27 * @param URL * Send request URL * @param param *     Request parameters, request parameters should be in the form of name1=value1&name2=value2.        * The response result of the remote resource represented by the @return URL * @ return Value: String * @ Exception Description: */public string Sendget (string URL, string param) {        String result = "";        BufferedReader in = null;        string[] proxylist = This.getproxy ();            if (null! = proxylist) {system.setproperty ("Http.proxyset", "true");            System.setproperty ("Http.proxyhost", proxylist[0]);        System.setproperty ("Http.proxyport", proxylist[1]);            } try {String urlnamestring = URL + "?" + param; URL RealuRL = new URL (urlnamestring);            The connection between open and URL urlconnection connection = Realurl.openconnection ();            Set the generic request attribute Connection.setrequestproperty ("accept", "*/*");            Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1;            SV1) ");            Establish the actual connection connection.connect ();            Get all response header fields Map<string, list<string>> Map = Connection.getheaderfields (); Traverse all response header fields for (String Key:map.keySet ()) {System.out.println (key + "--->" + map.get (key            )); }//define BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (connection.getinput            Stream ()));            String Line;            while (line = In.readline ()) = null) {result + = line; }} catch (Exception e){System.out.println ("Send GET request exception!            "+ e);        E.printstacktrace (); }//Use finally block to close the input stream finally {try {if (in! = null) {In.clos                E ();            }} catch (Exception E2) {e2.printstacktrace ();    }} return result; }/** * @ Method Name: Sendpost * Description: A request to send a POST method to a specified URL * @ Creator: <a Href=mailto: [email protected]> 's brother-in-law &     Lt;/a> * @ Modified By: * @ Modified: August 18, 2014 morning 9:29:09 * @param URL * Send request URL * @param param     * Request parameters, request parameters should be in the form of name1=value1&name2=value2.  * The response result of the remote resource represented by the @return * @throws Exception * @ return Value: String * @ Exception Description: */public string sendpost (string URL,        String param) throws Exception {printwriter out = null;        BufferedReader in = null;        String result = "";        string[] proxylist = This.getproxy ();            if (null! = proxylist) {System.setproperty ("Http.proxyset", "true");            System.setproperty ("Http.proxyhost", proxylist[0]);        System.setproperty ("Http.proxyport", proxylist[1]);            } try {url realurl = new URL (URL);            The connection between open and URL URLConnection conn = realurl.openconnection ();            Set the generic request attribute Conn.setrequestproperty ("accept", "*/*");            Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1;            SV1) ");            The Send POST request must be set to the following two lines conn.setdooutput (true);            Conn.setdoinput (TRUE);            Gets the output stream corresponding to the URLConnection object out = new PrintWriter (Conn.getoutputstream ());            Send request parameter out.print (param);            Flush output Stream Buffer Out.flush (); Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));            String Line;            while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {throw new MyException ("Send POST request exception!            "+ e); SYSTEM.OUT.PRINTLN ("Send POST request exception!")            "+ e);        E.printstacktrace (); }//Use finally block to close the output stream, input stream finally {try {if (out! = null) {OU                T.close ();                } if (in = null) {in.close (); }} catch (IOException ex) {throw new MyException ("Send POST request exception!                "+ ex");            Ex.printstacktrace ();    }} return result;     }/** * @ Method Name: Plusnum * Description: Number of Bus path count * @ Creator: <a Href=mailto: [email protected]> sister-in-law </a> * @ Modified By: * @ Modified: August 18, 2014 9:29:53 * @ return value: void * @ Exception Description: */public void Plusnum () {Shimengren    . num++; }    /**     *@ Method Name: Reductionnum * Description: Number of bus threads * @ Creator: <a Href=mailto: [email protected]> sister-in-law's brother-in-law </a> * Modified by: * @ Modified: August 18, 2014 9:30:03 * @ return value: void * @ Exception Description: */public void Reductionnum () {shimengren.num-        -; Shimengren.num = Shimengren.num < 0?    0:shimengren.num; }/** * @ Method Name: Plusthreadnum * Description: Concurrent threads count (not actually concurrency), use synchronous identifiers to prevent multi-threaded interference * @ Creator: <a href=mailto: [email p Rotected]> 's brother-in-law </a> * Modified by: * @ Modified: August 18, 2014 9:30:22 * @ return value: void * @ Exception Description: */public s    ynchronized void Plusthreadnum () {shimengren.threadnum++; }/** * @ Method Name: Reductionthreadnum * Description: Concurrent threads count (not actually concurrency), use synchronous identifiers to prevent multi-threaded interference * @ Creator: <a href=mailto: [EMAIL&N Bsp;protected]> 's brother-in-law </a> * Modified by: * @ Modified: August 18, 2014 9:30:28 * @ return value: void * @ Exception Description: */Pub        Lic synchronized void Reductionthreadnum () {shimengren.threadnum--; Shimengren.threadnum = Shimengren.threadnuM < 0?    0:shimengren.threadnum; }/** * @ Method Name: Plusview * Description: Browse count, use sync identifier, prevent multi-threaded interference * @ Creator: <a href=mailto: [email protected]> Brother-in-law </a> * @ Modified By: * @ Modified: August 18, 2014 9:30:33 * @ return value: void * @ Exception Description: */public synchronized void    Plusview () {shimengren.view++; }/** * @ Method Name: Runshimengren * Description: Call Sendpost method, and various counts * @ Creator: <a href=mailto: [email protected]> Small     Aunt's brother-in-law </a> * Modified by: * @ Modified: August 18, 2014 a.m. 9:30:37 * @throws MyException * @throws interruptedexception * @ return value: void * @ Exception Description: */public void Runshimengren () throws MyException, interruptedexception {this.        Plusnum ();            try {thread.sleep (New Random (). Nextint (2000)); if (Shimengren.max_thread_count > This.getthreadnum ()) {this.plusthreadnum ();//Counter self-increment S Tring result = Sendpost (This.url, This.param); Brush views Sendpost (This.urlzan, THIS.PAram);                    Brush the number of likes if (Result.equals ("1")) {This.plusview ();                This.jennylog ("Views:" + this.getview () + "\ n");            }}} catch (Exception e) {thread.sleep (2000);        throw new MyException ("site exception, stop all Threads.");                    } finally {System.out.println ("Waiting to execute thread queue:" + this.getnum () + ", current number of async threads:" + this.getthreadnum ()            + ", Views:" + This.getview ());            This.reductionnum ();        This.reductionthreadnum (); }}/** * @ Method Name: Jennylog * Description: Sister-in-the-diary, only to see the changes in the number of views * @ Creator: <a href=mailto: [email protected]>     Brother-in-law </a> * @ Modified By: * @ Modified: August 18, 2014 9:30:52 * @param data * @throws IOException * @ return value: void * @ Exception Description: */public void Jennylog (String data) throws IOException {File File = new file ("/home/beyond/tmp/j        Enny.log.txt ");        File File = new file ("E:\\jenny.log.txt"); if (!filE.getparentfile (). exists ()) {File.getparentfile (). Mkdirs ();        } Writer out = new FileWriter (file);        Out.write (data);    Out.close ();      }/** * <p> * Theme: Run * </p> * <p> * Description: Desperately in the dead post submission data * </p> * * @see Java.lang.runnable#run () */@Override public void Run () {for (;;)            {try {this.runshimengren ();            } catch (Exception e) {System.out.println ("send POST request exception."); }}}}/** * @ class Name: Jenny * Description: Sister-in-law class * @ Creator: <a Href=mailto: [email protected]> sister-in-law's brother-in-law </a> * Modified by: * @ Modified: August 1, 2014 5:53:48 * Modify Description:<br/> * @ Version information:v1.0.0<br/> */public class Jennythread {/** * @ Method Name: Mai N * Description: The main method of the sister-in-law * @ Creator: <a Href=mailto: [email protected]> 's sister-in-law </a> * Modified by: * @ Modified: 2014  August 1 Afternoon 5:53:48 * @param args * @throws Exception * @ return value: void * @ Exception Description: */  public static void Main (string[] args) {Runnable t = new Shimengren ();        for (int i = 0; i <; i++) {New Thread (T, "thread-" + i). Start (); }    }}


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.