本文包含代码,小程序中查看效果不佳,请切换至web模式。
import java.util.Random;
public class DemoUtl {
public static int index = 0;
//模拟不同ip 进行测试
public static void main(String[] args) throws InterruptedException {
try {
for (int i = 0; i < 100000; i++) {
Thread.sleep((new Random()).nextInt(1000) + 100);
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 100000; j++) {
try {
Thread.sleep((new Random()).nextInt(3200) + 1500);
test22 tt = new test22();
tt.sendPost(
"***URL***",
"***参***");//挂参post
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;
public class test22 {
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(),"utf-8"));
// StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
//sb.append(in.readLine());
result += line;
//System.out.println(sb.toString());
}
synchronized (this) {
DemoUtl.index = DemoUtl.index + 1;
}
System.out.println("第" + DemoUtl.index + "次服务请求; --> || 当前请求:" + param + " || 请求成功! || 模拟ip: " + ip
+ " || 返回结果Hash: " + result.toString().hashCode());
} catch (Exception 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);
}
}
博主辛苦了谢谢。
很喜欢您的文章谢谢。
这篇文章对我有用非常感谢。
希望看到更多优秀文章谢谢博主。
倍感珍惜谢谢.
对我很有用非常感谢.