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 }

java 模拟多ip访问的更多相关文章

  1. java模拟网页http-url访问

    package com.iflytek; import java.io.InputStream; import java.net.HttpURLConnection; import java.net. ...

  2. java模拟http请求(代理ip)

    java实现动态切换上网IP (ADSL拨号上网) java动态设置IP java模拟http的Get/Post请求 自动生成IP模拟POST访问后端程序 JAVA 动态替换代理IP并模拟POST

  3. java爬虫进阶 —— ip池使用,iframe嵌套,异步访问破解

    写之前稍微说一下我对爬与反爬关系的理解 一.什么是爬虫      爬虫英文是splider,也就是蜘蛛的意思,web网络爬虫系统的功能是下载网页数据,进行所需数据的采集.主体也就是根据开始的超链接,下 ...

  4. Jsoup实现java模拟登陆

    Jsoup实现java模拟登陆 2013-10-29 14:52:05|  分类: web开发|举报|字号 订阅     下载LOFTER我的照片书  |     1:如何获取cookies. 1.1 ...

  5. Java模拟登陆02【转载】

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢?     方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...

  6. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  7. Tomcat配置域名/IP访问及其中遇到的问题注意事项

    1.先在tomcat下的conf下找到server.xml文件,用记事本打开后,首先对端口号进行修改,以前一直以为8080是默认的端口号,其实默认的端口号是80 <Connector port= ...

  8. tomcat相关配置技巧梳理 (修改站点目录、多项目部署、限制ip访问、大文件上传超时等)

    tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomcat端口(可参考:分享一例测试环境下nginx+tomcat的视频业务部署记录)2)to ...

  9. 浏览器与服务器交互原理以及用java模拟浏览器操作v

    浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...

随机推荐

  1. [RN] React Native 使用 FlatList 实现九宫格布局 GridList

    React Native 使用 FlatList 实现九宫格布局 先看图片演示实例: 本文以图片列表为例,实现九宫格布局! 主要有两种方法: 1)方法一: 利用FlatList的 numColumns ...

  2. 手动停止jquery ajax请求

    主要调用jquery提供的ajax abort方法,详细代码如下: <html> <head> <meta charset="UTF-8"> & ...

  3. 微信小程序根据状态换图

    在index.wxml中添加图片 <image bindtap="click" src="{{show?'/images/.png':'/images/.png'} ...

  4. 无法定位程序输入点到xxx.dll

    Q:安装pytorch时报错无法定位程序输入点到Anaconda3\Library\bin\libssl-1_1-x64.dll A:下载libssl-1_1-x64.dll覆盖bin下的文件 下载地 ...

  5. 帝国cms7.5整合百度编辑器ueditor教程

    1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/download.html#ueditor 2.解压附件, ...

  6. 2015-2016-2《Java程序设计》团队博客4

    一.类结构图 这一周将所有的类都进行了实现,以下是这周实现的类图:         二.项目进展 目前已经将所有代码都编写完成,正在进行整体测试.虽然期间遇到了一些问题,但我们一起进行了讨论,并查找了 ...

  7. webpack构建篇

    WEBPack 构建  --  基于webpack4 1.环境准备 NodeJs: 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.其使用了一个事件驱动.非阻塞式 I/O 的模 ...

  8. [转]无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用

    刚学WinAPI编译遇到不少问题,LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用 MSVCRTD.lib test. 这个问题表明你新建 ...

  9. jmeter测试 flask 接口请求

    jmeter测试 flask 接口请求 flask的代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- from flask import Flas ...

  10. plsql查询数据库-中文显示问号问题

    解决方法: 设置本地环境变量 :NLS_LANG=AMERICAN_AMERICA.ZHS16GBK https://blog.csdn.net/github_38358734/article/det ...