java简单的http请求
一、先创建一个调用远程地址的类。
public class TestService {
private static Random random = new Random();
/**
* 接口调用 POST
*/
//第一种方法(但一个参数有多个值时)
public static boolean httpURLConnectionPOST(String i)throws Exception{
String outputStr = null;
StringBuffer buffer = null;
//调用请求地址
String url = "https://xxx.com?a=1";
StringBuffer result = new StringBuffer();
//连接
HttpURLConnection connection = null;
OutputStream os = null;
InputStream is = null;
BufferedReader br = null;
try {
//创建连接对象
URL ur = new URL(url);
//创建连接
connection = (HttpURLConnection) ur.openConnection();
//设置请求方法
connection.setRequestMethod("POST");
//设置连接超时时间
connection.setConnectTimeout(15000);
//设置读取超时时间
connection.setReadTimeout(15000);
//设置是否可读取
connection.setDoOutput(true);
//设置响应是否可读取
connection.setDoInput(true);
//设置参数类型
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置权限
//设置请求头等
//开启连接
//connection.connect();
//读取响应
System.out.println(connection.getResponseCode());
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String temp = null;
if ((temp = br.readLine()) != null) {
result.append(temp);
System.out.println(result);
}
}
}
return true;
//关闭连接MalformedURLExceptio
} catch (Exception e) {
System.out.print(e.getMessage());
throw new Exception(e.getMessage());
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
//关闭连接
connection.disconnect();
}
}
二、创建调用http接口类
public class MtTest {
private static Random random = new Random();//一个参数多个值,如需要用到随机数取值。
private static int threadCount = 1;// 线程数
private static final int count = 1;// 每线程执行次数
private static int interval = 100;//每次间隔时间
public static void main(String[] args) throws Exception {
final CountDownLatch latch = new CountDownLatch(threadCount);
final List<Long> costTimeList = new ArrayList<>(threadCount);
final Vector<Long> sucessList = new Vector<>(threadCount);
List<String> token=new ArrayList<String>();//保存参数值
token.add("1");
token.add("2");
token.add("3");
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
long s;
long e;
long total = 0;
long sucessCount = 0;
for (int j = 0; j < count; j++) {
s = System.currentTimeMillis();
try {
// 运行
int n = random.nextInt(token.size());//根据上文中保存的参数值,随机取一个
TestService.httpURLConnectionPOST(token.get(n));//调用http请求
sucessCount++;
} catch (Exception e1) {
e1.printStackTrace();
}
e = System.currentTimeMillis();
total += (e - s);
try {
Thread.sleep(interval);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
costTimeList.add(total);
sucessList.add(sucessCount);
latch.countDown();
}).start();
}
latch.await();// 等待所有线程执行完毕
final long sucessTotal = sucessList.stream().mapToLong(Long::longValue).sum();
final long totla = threadCount * count;
final long avgCost = costTimeList.stream().mapToLong(Long::longValue).sum() / totla;
final float tps = (float) threadCount / ((float) avgCost / (float) 1000);
System.out.println("=========================================执行结束,运行统计===============================");
System.out.println("并发线程数:" + threadCount);
System.out.println("每线程执行次数:" + count);
System.out.println("总执行次数:" + totla + ";成功:" + sucessTotal + ";失败:" + (totla - sucessTotal));
System.out.println("平均耗时:" + avgCost + "ms");
System.out.println("TPS:" + tps);
}
}

浙公网安备 33010602011771号