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

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

最新文章

  1. CDCE913产生任意频率
  2. &lt;转&gt;boost 1.53 and STLPort build binary for windows
  3. Greedy:Subsequence(POJ 3061)
  4. android布局详解
  5. (转)mysql账号权限密码设置方法
  6. linux下遍历目录(转-在于思考)
  7. JB开发之二 [jailbreak,越狱开发研究]
  8. cocos2dx 音效 粒子 数据存储
  9. 懒省事的小明--nyoj55
  10. Linux下开启nfs服务
  11. 微信小程序怎么做出前端table的效果
  12. Activiti的全局事件机制及其监听处理
  13. 基于Spring的轻量级工作流框架
  14. 传输层socket通讯之java实现
  15. [CVPR 2017] Semantic Autoencoder for Zero-Shot Learning论文笔记
  16. Jmeter生成html报告
  17. Python if __name__ == &#39;__main__&#39;:
  18. ajax 的一些参数
  19. 微信小程序des加密、PHP des解密
  20. android中使用spinner组件

热门文章

  1. Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收
  2. C#使用mybatis学习笔记
  3. 如何使用windows的计划任务?计划任务?
  4. FileOutputSteam入门
  5. Java编程的逻辑 (71) - 显式锁
  6. IDEA &amp; Android Studio换主题背景
  7. Python3 turtle安装和使用教程
  8. 在 Vim 中优雅地查找和替换
  9. 安装redis时候的坑
  10. 利用transform的bug使fixed相对于父级定位