Java中使用多线程、curl及代理IP模拟post提交和get访问

Java中使用多线程、curl及代理IP模拟post提交和get访问

菜鸟,多线程好玩就写着玩,大神可以路过指教,小弟在这受教,谢谢!

更多分享请关注微信公众号:lvxing1788

~~~~~~ 分割线扭起来 ~~~~~~

  1. /**  
  2.  * @组件名:javaDemo 
  3.  * @包名:javaDemo 
  4.  * @文件名:Jenny.java 
  5.  * @创建时间: 2014年8月1日 下午5:53:48 
  6.  * @版权信息:Copyright © 2014 eelly Co.Ltd,小姨子版权所有。 
  7.  */  
  8.   
  9. package javaDemo;  
  10.   
  11. import java.io.BufferedReader;  
  12. import java.io.File;  
  13. import java.io.FileWriter;  
  14. import java.io.IOException;  
  15. import java.io.InputStreamReader;  
  16. import java.io.PrintWriter;  
  17. import java.io.Writer;  
  18. import java.net.URL;  
  19. import java.net.URLConnection;  
  20. import java.util.List;  
  21. import java.util.Map;  
  22. import java.util.Random;  
  23.   
  24. /** 
  25.  * 用于异常的抛出而定义,因为Exception不能抛出,只有子类才能 
  26.  */  
  27. class MyException extends Exception {  
  28.   
  29.     private static final long serialVersionUID = -1485825968429580934L;  
  30.   
  31.     public MyException() {  
  32.         super();  
  33.     }  
  34.   
  35.     public MyException(String msg) {  
  36.         super(msg);  
  37.     }  
  38.   
  39.     public MyException(String msg, Throwable cause) {  
  40.         super(msg, cause);  
  41.     }  
  42.   
  43.     public MyException(Throwable cause) {  
  44.         super(cause);  
  45.     }  
  46. }  
  47.   
  48. /** 
  49.  * 一个小姨子需要的线程类 
  50.  */  
  51. class ShiMengRen implements Runnable {  
  52.   
  53.     /** 
  54.      * 浏览量的链接 
  55.      */  
  56.     private String url = "http://www.*******.com/article/yj/views.jhtml";  
  57.   
  58.     /** 
  59.      * 点赞的链接 
  60.      */  
  61.     private String urlZan = "http://www.********.com/article/yj/ups.jhtml";  
  62.   
  63.     /** 
  64.      * 游记的ID 
  65.      */  
  66.     private String param = "article_id=10844";  
  67.   
  68.     /** 
  69.      * 多线程共享的全局变量 
  70.      */  
  71.     // private volatile boolean flag = true;  
  72.   
  73.     /** 
  74.      * 同时进入post提交的线程数 
  75.      */  
  76.     private static final int MAX_THREAD_COUNT = 25;  
  77.   
  78.     /** 
  79.      * 同时进入post提交的线程数的计数器 
  80.      */  
  81.     private volatile static int threadNum = 0;  
  82.   
  83.     /** 
  84.      * 计数 
  85.      */  
  86.     private static int num = 0;  
  87.   
  88.     /** 
  89.      * 浏览次数 
  90.      */  
  91.     private static int view = 0;  
  92.   
  93.     /** 
  94.      * 代理IP 
  95.      */  
  96.     private String[] proxyArray = { "127.0.0.1:8787", "115.239.210.199:80", "149.255.255.242:80",  
  97.             "124.172.242.175:80", "117.59.217.243:80", "183.234.59.89:18186", "117.59.217.236:80",  
  98.             "183.224.1.56:80", "120.202.249.230:80", "125.46.100.198:9999", "112.65.19.122:8080",  
  99.             "202.96.172.234:808", "183.224.1.114:80", "183.224.1.113:80", "222.66.115.229:80" };  
  100.   
  101.     // public void setFlag(boolean flag) {  
  102.     // this.flag = flag;  
  103.     // }  
  104.     //  
  105.     // public boolean getFlag() {  
  106.     // return this.flag;  
  107.     // }  
  108.     //  
  109.     // public void stop() {  
  110.     // this.setFlag(false);  
  111.     // }  
  112.     //  
  113.     // public void start() {  
  114.     // this.setFlag(true);  
  115.     // }  
  116.   
  117.     public void setNum(int num) {  
  118.         ShiMengRen.num = num;  
  119.     }  
  120.   
  121.     public int getNum() {  
  122.         return ShiMengRen.num;  
  123.     }  
  124.   
  125.     public void setThreadNum(int threadNum) {  
  126.         ShiMengRen.threadNum = threadNum;  
  127.     }  
  128.   
  129.     public int getThreadNum() {  
  130.         return ShiMengRen.threadNum;  
  131.     }  
  132.   
  133.     public int getView() {  
  134.         return ShiMengRen.view;  
  135.     }  
  136.   
  137.     /** 
  138.      * @方法名:getProxy 
  139.      * @描述:随机获取代理IP 
  140.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  141.      * @修改人: 
  142.      * @修改时间:2014年8月18日 上午9:23:36 
  143.      * @return 
  144.      * @返回值:String[] 
  145.      * @异常说明: 
  146.      */  
  147.     public String[] getProxy() {  
  148.         String[] strlist = null;  
  149.         int len = proxyArray.length - 1;  
  150.         int num = 0;  
  151.         if (0 < len) {  
  152.             num = new Random().nextInt(len);  
  153.         }  
  154.         String proxy = this.proxyArray[num];  
  155.         if (proxy != "") {  
  156.             strlist = proxy.split(":");  
  157.         }  
  158.   
  159.         return strlist;  
  160.     }  
  161.   
  162.     /** 
  163.      * @方法名:sendGet 
  164.      * @描述:向指定URL发送GET方法的请求 
  165.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  166.      * @修改人: 
  167.      * @修改时间:2014年8月18日 上午9:26:27 
  168.      * @param url 
  169.      *            发送请求的URL 
  170.      * @param param 
  171.      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 
  172.      * @return URL 所代表远程资源的响应结果 
  173.      * @返回值:String 
  174.      * @异常说明: 
  175.      */  
  176.     public String sendGet(String url, String param) {  
  177.         String result = "";  
  178.         BufferedReader in = null;  
  179.   
  180.         String[] proxyList = this.getProxy();  
  181.         if (null != proxyList) {  
  182.             System.setProperty("http.proxySet", "true");  
  183.             System.setProperty("http.proxyHost", proxyList[0]);  
  184.             System.setProperty("http.proxyPort", proxyList[1]);  
  185.         }  
  186.         try {  
  187.             String urlNameString = url + "?" + param;  
  188.             URL realUrl = new URL(urlNameString);  
  189.             // 打开和URL之间的连接  
  190.             URLConnection connection = realUrl.openConnection();  
  191.             // 设置通用的请求属性  
  192.             connection.setRequestProperty("accept", "*/*");  
  193.             connection.setRequestProperty("connection", "Keep-Alive");  
  194.             connection.setRequestProperty("user-agent",  
  195.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  196.             // 建立实际的连接  
  197.             connection.connect();  
  198.             // 获取所有响应头字段  
  199.             Map<String, List<String>> map = connection.getHeaderFields();  
  200.             // 遍历所有的响应头字段  
  201.             for (String key : map.keySet()) {  
  202.                 System.out.println(key + "--->" + map.get(key));  
  203.             }  
  204.             // 定义 BufferedReader输入流来读取URL的响应  
  205.             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
  206.             String line;  
  207.             while ((line = in.readLine()) != null) {  
  208.                 result += line;  
  209.             }  
  210.         } catch (Exception e) {  
  211.             System.out.println("发送GET请求出现异常!" + e);  
  212.             e.printStackTrace();  
  213.         }  
  214.         // 使用finally块来关闭输入流  
  215.         finally {  
  216.             try {  
  217.                 if (in != null) {  
  218.                     in.close();  
  219.                 }  
  220.             } catch (Exception e2) {  
  221.                 e2.printStackTrace();  
  222.             }  
  223.         }  
  224.   
  225.         return result;  
  226.     }  
  227.   
  228.     /**  
  229.      * @方法名:sendPost  
  230.      * @描述:向指定 URL 发送POST方法的请求  
  231.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a>  
  232.      * @修改人:  
  233.      * @修改时间:2014年8月18日 上午9:29:09  
  234.      * @param url  
  235.      *            发送请求的 URL  
  236.      * @param param  
  237.      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。  
  238.      * @return 所代表远程资源的响应结果  
  239.      * @throws Exception  
  240.      * @返回值:String  
  241.      * @异常说明:  
  242.      */  
  243.     public String sendPost(String url, String param) throws Exception {  
  244.         PrintWriter out = null;  
  245.         BufferedReader in = null;  
  246.         String result = "";  
  247.   
  248.         String[] proxyList = this.getProxy();  
  249.         if (null != proxyList) {  
  250.             System.setProperty("http.proxySet", "true");  
  251.             System.setProperty("http.proxyHost", proxyList[0]);  
  252.             System.setProperty("http.proxyPort", proxyList[1]);  
  253.         }  
  254.         try {  
  255.             URL realUrl = new URL(url);  
  256.             // 打开和URL之间的连接  
  257.             URLConnection conn = realUrl.openConnection();  
  258.             // 设置通用的请求属性  
  259.             conn.setRequestProperty("accept", "*/*");  
  260.             conn.setRequestProperty("connection", "Keep-Alive");  
  261.             conn.setRequestProperty("user-agent",  
  262.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  263.             // 发送POST请求必须设置如下两行  
  264.             conn.setDoOutput(true);  
  265.             conn.setDoInput(true);  
  266.             // 获取URLConnection对象对应的输出流  
  267.             out = new PrintWriter(conn.getOutputStream());  
  268.             // 发送请求参数  
  269.             out.print(param);  
  270.             // flush输出流的缓冲  
  271.             out.flush();  
  272.             // 定义BufferedReader输入流来读取URL的响应  
  273.             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  274.             String line;  
  275.             while ((line = in.readLine()) != null) {  
  276.                 result += line;  
  277.             }  
  278.         } catch (Exception e) {  
  279.             throw new MyException("发送 POST 请求出现异常!" + e);  
  280.             // System.out.println("发送 POST 请求出现异常!" + e);  
  281.             // e.printStackTrace();  
  282.         }  
  283.         // 使用finally块来关闭输出流、输入流  
  284.         finally {  
  285.             try {  
  286.                 if (out != null) {  
  287.                     out.close();  
  288.                 }  
  289.                 if (in != null) {  
  290.                     in.close();  
  291.                 }  
  292.             } catch (IOException ex) {  
  293.                 throw new MyException("发送 POST 请求出现异常!" + ex);  
  294.                 // ex.printStackTrace();  
  295.             }  
  296.         }  
  297.   
  298.         return result;  
  299.     }  
  300.   
  301.     /**  
  302.      * @方法名:plusNum  
  303.      * @描述:总线程数的计数  
  304.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a>  
  305.      * @修改人:  
  306.      * @修改时间:2014年8月18日 上午9:29:53  
  307.      * @返回值:void  
  308.      * @异常说明:  
  309.      */  
  310.     public void plusNum() {  
  311.         ShiMengRen.num++;  
  312.     }  
  313.   
  314.     /** 
  315.      * @方法名:reductionNum 
  316.      * @描述:总线程数的计数 
  317.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  318.      * @修改人: 
  319.      * @修改时间:2014年8月18日 上午9:30:03 
  320.      * @返回值:void 
  321.      * @异常说明: 
  322.      */  
  323.     public void reductionNum() {  
  324.         ShiMengRen.num--;  
  325.         ShiMengRen.num = ShiMengRen.num < 0 ? 0 : ShiMengRen.num;  
  326.     }  
  327.   
  328.     /** 
  329.      * @方法名:plusThreadNum 
  330.      * @描述:并发线程数计数(其实也不算是并发),使用同步标识符,防止多线程的干扰 
  331.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  332.      * @修改人: 
  333.      * @修改时间:2014年8月18日 上午9:30:22 
  334.      * @返回值:void 
  335.      * @异常说明: 
  336.      */  
  337.     public synchronized void plusThreadNum() {  
  338.         ShiMengRen.threadNum++;  
  339.     }  
  340.   
  341.     /** 
  342.      * @方法名:reductionThreadNum 
  343.      * @描述:并发线程数计数(其实也不算是并发),使用同步标识符,防止多线程的干扰 
  344.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  345.      * @修改人: 
  346.      * @修改时间:2014年8月18日 上午9:30:28 
  347.      * @返回值:void 
  348.      * @异常说明: 
  349.      */  
  350.     public synchronized void reductionThreadNum() {  
  351.         ShiMengRen.threadNum--;  
  352.         ShiMengRen.threadNum = ShiMengRen.threadNum < 0 ? 0 : ShiMengRen.threadNum;  
  353.     }  
  354.   
  355.     /** 
  356.      * @方法名:plusView 
  357.      * @描述:浏览量计数,使用同步标识符,防止多线程的干扰 
  358.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  359.      * @修改人: 
  360.      * @修改时间:2014年8月18日 上午9:30:33 
  361.      * @返回值:void 
  362.      * @异常说明: 
  363.      */  
  364.     public synchronized void plusView() {  
  365.         ShiMengRen.view++;  
  366.     }  
  367.   
  368.     /** 
  369.      * @方法名:runShiMengRen 
  370.      * @描述:调用sendPost的方法,以及各种计数 
  371.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  372.      * @修改人: 
  373.      * @修改时间:2014年8月18日 上午9:30:37 
  374.      * @throws MyException 
  375.      * @throws InterruptedException 
  376.      * @返回值:void 
  377.      * @异常说明: 
  378.      */  
  379.     public void runShiMengRen() throws MyException, InterruptedException {  
  380.         this.plusNum();  
  381.         try {  
  382.             Thread.sleep(new Random().nextInt(2000));  
  383.             if (ShiMengRen.MAX_THREAD_COUNT > this.getThreadNum()) {  
  384.                 this.plusThreadNum(); // 计数器自增  
  385.                 String result = sendPost(this.url, this.param); // 刷浏览数  
  386.                 sendPost(this.urlZan, this.param); // 刷点赞数  
  387.                 if (result.equals("1")) {  
  388.                     this.plusView();  
  389.                     this.jennyLog("浏览次数:" + this.getView() + " ");  
  390.                 }  
  391.             }  
  392.         } catch (Exception e) {  
  393.             Thread.sleep(2000);  
  394.             throw new MyException("网站出现异常,停止所有线程.");  
  395.         }  
  396.   
  397.         finally {  
  398.             System.out.println("等待执行线程队列:" + this.getNum() + ",当前异步线程数量:" + this.getThreadNum()  
  399.                     + ",浏览次数:" + this.getView());  
  400.             this.reductionNum();  
  401.             this.reductionThreadNum();  
  402.         }  
  403.     }  
  404.   
  405.     /** 
  406.      * @方法名:jennyLog 
  407.      * @描述:小姨子的日志记录,只为看浏览量的变化 
  408.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  409.      * @修改人: 
  410.      * @修改时间:2014年8月18日 上午9:30:52 
  411.      * @param data 
  412.      * @throws IOException 
  413.      * @返回值:void 
  414.      * @异常说明: 
  415.      */  
  416.     public void jennyLog(String data) throws IOException {  
  417.         File file = new File("/home/beyond/tmp/Jenny.log.txt");  
  418.         // File file = new File("E:\Jenny.log.txt");  
  419.         if (!file.getParentFile().exists()) {  
  420.             file.getParentFile().mkdirs();  
  421.         }  
  422.         Writer out = new FileWriter(file);  
  423.         out.write(data);  
  424.         out.close();  
  425.     }  
  426.   
  427.     /** 
  428.      * <p> 
  429.      * 主题:run 
  430.      * </p> 
  431.      * <p> 
  432.      * 描述:往死里拼命的Post提交数据 
  433.      * </p> 
  434.      *  
  435.      * @see java.lang.Runnable#run() 
  436.      */  
  437.     @Override  
  438.     public void run() {  
  439.         for (;;) {  
  440.             try {  
  441.                 this.runShiMengRen();  
  442.             } catch (Exception e) {  
  443.                 System.out.println("发送 POST 请求出现异常.");  
  444.             }  
  445.         }  
  446.     }  
  447. }  
  448.   
  449. /** 
  450.  * @类名:Jenny 
  451.  * @描述:小姨子类 
  452.  * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  453.  * @修改人: 
  454.  * @修改时间:2014年8月1日 下午5:53:48 
  455.  * @修改说明:<br/> 
  456.  * @版本信息:V1.0.0<br/> 
  457.  */  
  458. public class JennyThread {  
  459.   
  460.     /** 
  461.      * @方法名:main 
  462.      * @描述:小姨子的主方法 
  463.      * @创建人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> 
  464.      * @修改人: 
  465.      * @修改时间:2014年8月1日 下午5:53:48 
  466.      * @param args 
  467.      * @throws Exception 
  468.      * @返回值:void 
  469.      * @异常说明: 
  470.      */  
  471.     public static void main(String[] args) {  
  472.         Runnable t = new ShiMengRen();  
  473.         for (int i = 0; i < 60; i++) {  
  474.             new Thread(t, "Thread-" + i).start();  
  475.         }  
  476.     }  
  477. }  

~~~~~~ 分割线扭起来 ~~~~~~

原文地址:https://www.cnblogs.com/gisblogs/p/4200902.html