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

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

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

[java] view plaincopyprint?

  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.shimengren.com/article/yj/views.jhtml";
  47. /**
  48. * 点赞的链接
  49. */
  50. private String urlZan =
    "http://www.shimengren.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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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: [email protected]>小姨子的姐夫</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. }
时间: 2024-07-30 10:21:13

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

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

Java中使用多线程.curl及代理IP模拟post提交和get訪问 菜鸟,多线程好玩就写着玩.大神能够路过不吝赐教.小弟在这受教.谢谢! 很多其它分享请关注微信公众号:lvxing1788 ~~~~~~ 切割线扭起来 ~~~~~~ /** * @组件名:javaDemo * @包名:javaDemo * @文件名称:Jenny.java * @创建时间: 2014年8月1日 下午5:53:48 * @版权信息:Copyright ? 2014 eelly Co.Ltd,小姨子版权全部. */

Java中的多线程你只要看这一篇就够了

Java中的多线程你只要看这一篇就够了 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其实只有一半对,因为反应"多角色"的程序代码,最起码每个角色要给他一个线程吧,否则连实际场景都无法模拟,当然也没法说能用单线程来实现:比如最常见的"生产者,消费者模型". 很多人都对其中的一些概念不够明确,如同步.并发等等,让我

为什么Java中实现多线程的方式有两种?

在面试的过程中,我们经常问被面试者,为什么Java中实现多线程的方式有两种(一种是直接继承Thread类,一种是实现Runnable接口)?可惜的是,很多面试者都答不出来,甚至从来没有想为什么.,那么真正的原因是什么呢?我们可以用反证法推理一下: 假设Java只提供Thread供大家继承从而实现多线程,考虑下面的一个需求,如果有一个已经继承了某个父类的类,但是这个类又想实现多线程,怎么办?很显然,如果只提供一个可以继承的类,肯定解决不了这个问题.那么,如何解决,毫无疑问,就只能使用接口了.

Java中的 多线程编程

Java 中的多线程编程 一.多线程的优缺点 多线程的优点: 1)资源利用率更好2)程序设计在某些情况下更简单3)程序响应更快 多线程的代价: 1)设计更复杂虽然有一些多线程应用程序比单线程的应用程序要简单,但其他的一般都更复杂.在多线程访问共享数据的时候,这部分代码需要特别的注意.线程之间的交互往往非常复杂.不正确的线程同步产生的错误非常难以被发现,并且重现以修复. 2)上下文切换的开销当CPU从执行一个线程切换到执行另外一个线程的时候,它需要先存储当前线程的本地的数据,程序指针等,然后载入另

Java中的多线程=你只要看这一篇就够了

如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其实只有一半对,因为反应“多角色”的程序代码,最起码每个角色要给他一个线程吧,否则连实际场景都无法模拟,当然也没法说能用单线程来实现:比如最常见的“生产者,消费者模型”. 很多人都对其中的一些概念不够明确,如同步.并发等等,让我们先建立一个数据字典,以免产生误会. 多线程:指的是这个程序(一个进程)运

黑马程序员【Java中的多线程】

Java中的多线程 首先,在开篇讲线程之前要说一个问题,我们知道多线程的执行原理是cpu在不同的线程中做着切换操作,而一提到多线程,大家首先想到的肯定是提高系统的运行效率,可是真的是这样的么?我们来借助一位大神博客中的代码就可以看出来有时单线程的运行效率要高于多线程: import threading from time import ctime class MyThread(threading.Thread): def __init__(self, func, args, name): thr

java中实现多线程的两种基本方法

java中实现多线程有两种基本方法,一种是继承Thread, 另一种是实现Runnable接口. 但是因为java中子类只能继承一个父类,如果采用继承Thread类,就不能继承其他类,很受限制. 以下是采用继承Thread类的例子: public class MyThreadTest{ public static void main(String[] args){ MyThread amythread1=new MyThread("my thread 1"); MyThread amy

java 中的多线程

java中实现多线程的方式有两种: 1.实现继承Thread 类的类(重写run方法) 2.实现Runnable 接口(重写run方法) 上述两种方式的关系; 看过jdk你会发现 Thread类是实现了 runnable的接口了的   可见,实现Runnable接口相对于继承Thread类来说,有如下显著的优势: (1). 适合多个相同程序代码的线程去处理同一资源的情况,把虚拟CPU(线程)同程序的代码.数据有效分离,较好地体现了面向对象的设计思想. (2). 可以避免由于Java的单继承特性带

在java中获取URL的域名或IP与端口

在java中获取URL的域名或IP与端口 获取IP与域名方法一,使用正则表达式 public static String getIP(String url) { //使用正则表达式过滤, String re = "((http|ftp|https)://)(([a-zA-Z0-9._-]+)|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(([a-zA-Z]{2,6})|(:[0-9]{1,4})?)"; String str = &quo