FunTester httpclient 多线程爬虫实例

FunTester · 2020年03月29日 · 626 次阅读

本人最近在研究安全测试的过程中,偶然发现某站一个漏洞,在获取资源的时候竟然不需要校验,原来设定的用户每天获取资源的次数限制就没了。赶紧想到用爬虫多爬一些数据,但是奈何数据量太大了,所以想到用多线程来爬虫。经过尝试终于完成了,脚本写得比较粗糙,因为没真想爬完。预计 10 万数据量,10 个线程,每个线程爬 1 万,每次爬 100 个数据(竟然是 get 接口,有 url 长度限制)。

分享代码,供大家参考。

package practise;

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.http.client.methods.HttpGet;
import net.sf.json.JSONObject;
import source.ApiLibrary;

public class LoginDz extends ApiLibrary {

    public static void main(String[] args) {
        LoginDz loginDz = new LoginDz();
        loginDz.excuteTreads();
        testOver();
    }

    public JSONObject getTi(int[] code, String name) {
        JSONObject response = null;
        String url = "***********";
        JSONObject args = new JSONObject();
        // args.put("ID_List", getTiId(884969));
        args.put("ID_List", getTiId(code));
        HttpGet httpGet = getHttpGet(url, args);
        response = getHttpResponseEntityByJson(httpGet);
        // output(response.toString());
        String text = response.toString();
        if (!text.equals("{\"success_response\":[]}"))
            logLog("name", response.toString());
        output(response);
        return response;
    }


    public String getTiId(int... id) {
        StringBuffer result = new StringBuffer();
        int length = id.length;
        for (int i = 0; i < length; i++) {
            String abc = "filter[where][origDocID][inq]=" + id[i] + "&";
            result.append(abc);
        }
        return result.toString();
    }

    /**
     * 执行多线程任务
     */
    public void excuteTreads() {
        int threads = 10;
        ExecutorService executorService = Executors.newFixedThreadPool(threads);
        CountDownLatch countDownLatch = new CountDownLatch(threads);
        Date start = new Date();
        for (int i = 0; i < threads; i++) {
            executorService.execute(new More(countDownLatch, i));
        }
        try {
            countDownLatch.await();
            executorService.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Date end = new Date();
        outputTimeDiffer(start, end);
    }

    /**
     * 多线程类
     */
    class More implements Runnable {
        public CountDownLatch countDownLatch;
        public int num;

        public More(CountDownLatch countDownLatch, int num) {
            this.countDownLatch = countDownLatch;
            this.num = num;
        }

        @Override
        public void run() {
            int bound = num * 10000;

            try {
                for (int i = bound; i < bound + 10000; i += 100) {
                    int[] ids = new int[100];
                    for (int k = 0; k < 100; k++) {
                        ids[i] = i + k;
                        getTi(ids, bound + "");
                    }
                }
            } finally {
                countDownLatch.countDown();
            }
        }

    }

}

### 技术类文章精选

  1. java 一行代码打印心形
  2. Linux 性能监控软件 netdata 中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在 Linux 命令行界面愉快进行性能测试
  6. 图解 HTTP 脑图
  7. 如何测试概率型业务接口
  8. httpclient 处理多用户同时在线
  9. 将 swagger 文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient 如何处理 302 重定向
  12. 基于 java 的直线型接口测试框架初探
  13. Tcloud 云测平台 -- 集大成者

非技术文章精选

  1. 为什么选择软件测试作为职业道路?
  2. 成为杰出 Java 开发人员的 10 个步骤
  3. 写给所有人的编程思维
  4. 自动化测试的障碍
  5. 自动化测试的问题所在
  6. 测试之《代码不朽》脑图
  7. 成为优秀自动化测试工程师的 7 个步骤
  8. 优秀软件开发人员的态度
  9. 如何正确执行功能 API 测试

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册