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

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

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值