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();
		}

	}
}


java模拟多ip发送post请求
要使用 Java 模拟多 IP 发送 POST 请求,可以使用代理 IP 来实现。以下是一个示例代码,演示了如何设置代理 IP 发送 HTTP POST 请求:
首先,确保你已经导入了相关的网络请求库,如java.net.HttpURLConnection或其他 HTTP 客户端库(如 Apache HttpComponents 等)。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class ProxyPostRequestExample {
    public static void main(String[] args) {
        String targetUrl = "http://example.com/api"; // 替换为实际的目标 URL
        String proxyHost = "proxy-host"; // 替换为代理服务器的主机或 IP 地址
        int proxyPort = 8080; // 替换为代理服务器的端口

        for (int i = 0; i < 5; i++) { // 模拟发送 5 次请求
            sendPostRequestWithProxy(targetUrl, proxyHost, proxyPort);
        }
    }

    public static void sendPostRequestWithProxy(String targetUrl, String proxyHost, int proxyPort) {
        try {
            URL url = new URL(targetUrl);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection(new java.net.Proxy(java.net.Proxy.Type.HTTP, new java.net.InetSocketAddress(proxyHost, proxyPort)));

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            String postData = "param1=value1&param2=value2"; // 替换为实际的 POST 数据

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(postData);
            writer.flush();

            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine())!= null) {
                    response.append(line);
                }
                reader.close();

                System.out.println("Response: " + response.toString());
            } else {
                System.out.println("Request failed with response code: " + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上述示例中,定义了一个sendPostRequestWithProxy方法,它接受目标 URL、代理服务器的主机和端口作为参数。
在方法内部,通过创建java.net.Proxy对象并设置到URLConnection中,来指定使用的代理服务器。然后设置请求方法为POST,并设置doOutput为true以发送 POST 数据。
接下来,构造要发送的 POST 数据,并使用OutputStreamWriter将数据写入连接的输出流中。
最后,获取服务器的响应码,并根据响应码读取响应内容或输出错误信息。
请注意,上述代码中的代理服务器的主机(proxyHost)和端口(proxyPort)需要替换为实际可用的代理服务器信息。此外,还需要根据实际情况设置请求头、处理请求参数等。
另外,使用代理 IP 时需要注意以下几点:
确保你有合法使用代理服务器的权限。
代理服务器的稳定性和性能可能会影响请求的结果。
一些网站可能会检测和限制使用代理的访问,或者对来自特定 IP 范围的请求有特定的规则和限制。
如果需要更复杂的请求设置或处理,可能需要使用更强大的 HTTP 客户端库,如 Apache HttpComponents 或其他类似的库,它们提供了更丰富的功能和更灵活的配置选项来满足各种需求。
同时,随机生成大量不同的 IP 地址来模拟请求可能违反网站的使用条款和服务协议,并且在某些情况下可能被视为不合法或不道德的行为。请确保你的使用方式是合规和道德的,并尊重目标网站的规定和限制。此外,某些网站可能会采取措施来检测和防范此类行为,例如限制频繁的请求或使用 IP 封锁等技术。在进行任何网络请求活动时,建议遵循良好的网络行为准则,并根据实际需求和合法目的进行操作。如果你的目的是合法的测试或数据收集,最好与目标网站的所有者或管理员进行沟通,以确保你的行为得到适当的授权和认可。

要在 Java 中模拟多 IP 发送 POST 请求并添加请求头,可以使用HttpURLConnection类来实现。以下是一个示例代码,演示了如何设置请求头并发送 POST 请求:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MultiIPPostRequestWithHeaders {
    public static void main(String[] args) throws IOException {
        String url = "http://example.com/api"; 
        // 替换为实际的目标 URL

        // 模拟多个 IP(这里仅设置了一个示例 IP)
        String[] ips = {"192.168.0.100"}; 

        for (String ip : ips) {
            sendPostRequestWithHeaders(url, ip);
        }
    }

    public static void sendPostRequestWithHeaders(String url, String ip) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

        // 设置请求方法为 POST
        connection.setRequestMethod("POST");

        // 设置请求头
        connection.setRequestProperty("Content-Type", "application/json"); 
        connection.setRequestProperty("Authorization", "Bearer yourAccessToken"); 
        // 添加其他需要的请求头

        // 设置特定的 IP(仅为示例,实际中可能需要更复杂的方式来设置多个 IP)
        connection.setRequestProperty("X-Forwarded-For", ip); 

        // 设置允许输出
        connection.setDoOutput(true);

        String requestBody = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; 
        // 替换为实际的请求体 JSON 数据

        try (OutputStream outputStream = connection.getOutputStream()) {
            outputStream.write(requestBody.getBytes());
            outputStream.flush();
        }

        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine())!= null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println("Response from " + ip + ": " + response.toString());
        } else {
            System.out.println("POST request to " + url + " failed with response code: " + responseCode);
        }
    }
}

在上述示例中,首先设置了要请求的目标 URL。然后,在sendPostRequestWithHeaders方法中进行了以下操作:
创建HttpURLConnection对象并设置请求方法为POST。
设置了常见的请求头,如Content-Type和Authorization,你可以根据实际需求添加其他请求头。
通过setRequestProperty(“X-Forwarded-For”, ip)设置了X-Forwarded-For请求头来模拟不同的 IP 地址。请注意,这只是一种简单的模拟方式,实际中设置多个 IP 可能需要更复杂的网络配置或使用代理等方式。
设置允许输出,并将请求体数据写入输出流。
获取响应码并根据响应码读取响应内容或输出错误信息。
请将"http://example.com/api"替换为你实际要发送 POST 请求的目标网址,并根据需要修改请求头和请求体的数据。
另外,模拟多个 IP 发送请求的具体实现方式可能会受到网络环境和应用场景的限制。在某些情况下,可能需要使用代理服务器或者特定的网络设置来实现更真实的多 IP 请求。同时,还需注意遵守相关的法律规定和网站的使用条款,确保你的请求行为是合法和合规的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值