java 模擬多ip訪問


java模擬多ip請求


package url_demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

public class HttpUtilTest {

	private int index = 0;

	public String sendPost(String url, String param) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			URLConnection conn = realUrl.openConnection();

			// 隨機生成ip
			String ip = randIP();
			conn.setRequestProperty("X-Forwarded-For", ip);
			conn.setRequestProperty("HTTP_X_FORWARDED_FOR", ip);
			conn.setRequestProperty("HTTP_CLIENT_IP", ip);
			conn.setRequestProperty("REMOTE_ADDR", ip);
			conn.setRequestProperty("Host", "");
			conn.setRequestProperty("Connection", "keep-alive");
			conn.setRequestProperty("Content-Length", "17");
			conn.setRequestProperty("Accept", "application/json");
			conn.setRequestProperty("Origin", "ORIGIN");
			conn.setRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("Referer", "REFERER");
			conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4,pt;q=0.2");

			conn.setDoOutput(true);
			conn.setDoInput(true);
			out = new PrintWriter(conn.getOutputStream());
			out.print(param);
			out.flush();
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			synchronized (this) {
				DemoUtl.index = DemoUtl.index + 1;
			}
			System.out.println("第" + DemoUtl.index + "次訪問; -->  || 當前線程:" + param + "  || 請求成功! || 模擬ip: " + ip
					+ " || 返回結果: " + result.toString().hashCode());
		} catch (Exception e) {
			// System.out.println("發送 POST 請求出現異常!" + e);
			// e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	public static String randIP() {
		Random random = new Random(System.currentTimeMillis());
		return (random.nextInt(255) + 1) + "." + (random.nextInt(255) + 1) + "." + (random.nextInt(255) + 1) + "."
				+ (random.nextInt(255) + 1);
	}
}

package url_demo;

import java.util.Random;

public class DemoUtl {

	public static int index = 0;

	public static void main(String[] args) throws InterruptedException {
		try {
			for (int i = 0; i < 100000; i++) {
				Thread.sleep((new Random()).nextInt(200) + 100);
				new Thread(new Runnable() {
					@Override
					public void run() {

						for (int j = 0; j < 100000; j++) {
							try {
								Thread.sleep((new Random()).nextInt(3200) + 1500);
								HttpUtilTest tt = new HttpUtilTest();
								tt.sendPost(
										"https://www.baidu.com",
										Thread.currentThread().getName());
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}).start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


 public static void main(String[] args) throws InterruptedException {
        ExpiryMap<String, Object> map = ExpiryMap.getInstance();
    
        // 往map寫入1-1000, key和value相同
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 10; i++) {
                    map.put(i + "", i, i * 200);
                }
            }
        };

        int threadNum = 2;// 線程數
//        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < threadNum; i++) {
            Thread thread = new Thread(runnable);
//            threadList.add(thread);
            thread.start();
        }

//        // 主線程等待子線程執行完成
//        for (Thread thread : threadList) {
//            try {
//                thread.join();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
 
//        System.out.println(map.size());// 結果可能大於1000

 
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM