java使用ip代理池模拟不同地区客户端访问

前言

    有时候需要快速提高自己一篇文章的活跃度,对于一些没有身份认证的只靠ip和http header统计访问量的系统,我们可以使用脚本+ip代理池来进行模拟访问

代码

proxy文件为楼主存储ip代理池的文件,ip代理池可以从网上找ip获取或者直接找一份长期的ip存在本地,楼主用的是后者(需要的ip量不大,可以重复),格式如下:

58.218.200.223:30352
58.218.200.223:30303
58.218.200.223:30085
58.218.200.223:30366
58.218.200.223:30155
58.218.200.223:30417

下面是简易的代码,当然你也可以随机添加一些请求头

import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class NowCodeAccessScript {

    private static final ExecutorService es = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws IOException {
//        if (args.length < 1){
//            return;
//        }
//        String url = args[1];
        //获取代理
        BufferedReader br = new BufferedReader(new FileReader("proxy"));
        String ip = null;
        List<IP> ipList = new ArrayList<>();
        while((ip = br.readLine())!=null){
            int splitIdx  = ip.indexOf(":");
            String ipstr = ip.substring(0, splitIdx);
            String port = ip.substring(splitIdx+1);
            IP ipD = new IP(ipstr, Integer.valueOf(port));
            ipList.add(ipD);
        }
		//代理浏览的网址
        String url = "https://www.nowcoder.com/discuss/203238";
        AtomicLong totalCurl = new AtomicLong(0);

        for (int i=0; i<5; i++){
            es.submit(()->{int t =1000;while(t-->0)curlUrl(url,true,ipList,totalCurl);});
        }

        while(!es.isTerminated()){
            Thread.yield();
        }
    }
    static class IP{
        String ip;
        int port;
        public IP(String ip, int port){
            this.ip = ip;
            this.port = port;
        }

    }

    public static void curlUrl(String url, boolean useProxy, List<IP> ipList, AtomicLong totalCurl){
        try {
            Random random = new Random();
            IP ip = ipList.get(random.nextInt(ipList.size()));

            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip.ip, ip.port));
            HttpURLConnection conn = null;

            if (useProxy){
                conn = (HttpURLConnection) new URL(url).openConnection(proxy);
            }else{
                conn = (HttpURLConnection) new URL(url).openConnection();
            }

            conn.setRequestMethod("GET");
            conn.connect();
            conn.getInputStream();
            conn.disconnect();
            try {
                Thread.sleep(new Random().nextInt(4000)+1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("访问量:"+totalCurl.addAndGet(1));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

你可能感兴趣的:(java使用ip代理池模拟不同地区客户端访问)