java 模拟多ip访问
2019-09-08 09:50:52来源:博客园 阅读 ()
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();
}
}
}
原文链接:https://www.cnblogs.com/eian/p/11478409.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Java自学-类和对象 单例模式
下一篇:disruptor
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash