当前位置: 首页 > news >正文

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();// 随机生成ipString 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() {@Overridepublic 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"; // 替换为实际的目标 URLString 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();// 设置请求方法为 POSTconnection.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 请求。同时,还需注意遵守相关的法律规定和网站的使用条款,确保你的请求行为是合法和合规的。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 基于STM32的农业大棚温湿度采集控制系统的设计
  • git实现多人协作开发同一个项目
  • 100个python的基本语法知识【下】
  • 【洛谷】P2241 统计方形(数据加强版)——C++
  • dockerfile部署wordpress
  • 【CTFWP】ctfshow-萌新-web1-7
  • html+css前端作业 王者荣耀官网6个页面无js
  • iOS应用冷启动优化,可以做哪些事情
  • ArcGIS Pro SDK (九)几何 12 多面体
  • 决策树的概念
  • 挖掘IPython的深度:%%dhist命令的历史探索之旅
  • Chapter17 表面着色器——Shader入门精要学习
  • keystone学习小结2
  • 深入理解Linux网络(四):TCP接收阻塞
  • 前端 Tips
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Fundebug计费标准解释:事件数是如何定义的?
  • leetcode46 Permutation 排列组合
  • magento 货币换算
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • Python3爬取英雄联盟英雄皮肤大图
  • SpiderData 2019年2月13日 DApp数据排行榜
  • sublime配置文件
  • webpack4 一点通
  • webpack入门学习手记(二)
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 来,膜拜下android roadmap,强大的执行力
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 我这样减少了26.5M Java内存!
  • 你对linux中grep命令知道多少?
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #pragma data_seg 共享数据区(转)
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • $(function(){})与(function($){....})(jQuery)的区别
  • $(selector).each()和$.each()的区别
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (el-Date-Picker)操作(不使用 ts):Element-plus 中 DatePicker 组件的使用及输出想要日期格式需求的解决过程
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (独孤九剑)--文件系统
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (强烈推荐)移动端音视频从零到上手(上)
  • (十三)Flask之特殊装饰器详解
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • .net core + vue 搭建前后端分离的框架
  • .NET 某和OA办公系统全局绕过漏洞分析
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .Net中的集合
  • @Autowired注解的实现原理
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [100天算法】-每个元音包含偶数次的最长子字符串(day 53)
  • [Android 13]Input系列--获取触摸窗口
  • [Android Pro] listView和GridView的item设置的高度和宽度不起作用