如果一个接口需要在另一个接口请求时完成操作,即在另一个接口响应前完成请求时,并行执行的情况下
选择从API定义导入选择想要的API或者CASE
环境和请求报文格式同时生成
我这里没用metersphere给的代码,自己找的
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//创建连接
private static String sendPostCreateChannel(String apiUrl,String jsonInputString){
String responsecreate="";
try{
URL url=new URL(apiUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方法为POST
connection.setRequestMethod("POST");
//设置请求头
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","*/*");
connection.setRequestProperty("App_Id","KC_ICS");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("ClientId","dddd");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Host","173.9.9.3:9089");
connection.setRequestProperty("Postman-Token","erer");
//启用输入输出流
connection.setDoOutput(true);
//写入JSON数据
OutputStream os=connection.getOutputStream();
byte[] input=jsonInputString.getBytes("utf-8");
os.write(input,0,input.length);
//获取返回的数据
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder response=new StringBuilder();
String responseLIne;
while((responseLine=br.readLine()) !=null){
response.append(responseLIne.trim());
}
log.info("response.toString"+response.toString());
responsecreate=response.toString();
connection.disconnect();
}catch(IOException e){
e.printStackTrace();
}
return responsecreate
}
//创建连接
private static String sendPostSendMessage(String apiUrl,String jsonInputString){
String resultMsg="";
try{
URL url=new URL(apiUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方法为POST
connection.setRequestMethod("POST");
//设置请求头
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","*/*");
connection.setRequestProperty("App_Id","KC_ICS");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("ClientId","dddd");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Host","localhost:8081");
connection.setRequestProperty("Postman-Token","erer");
connection.setRequestProperty("Trand_Id","34343");
//启用输入输出流
connection.setDoOutput(true);
//写入JSON数据
OutputStream os=connection.getOutputStream();
byte[] input=jsonInputString.getBytes("utf-8");
os.write(input,0,input.length);
//获取返回的数据
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder response=new StringBuilder();
String responseLIne;
while((responseLine=br.readLine()) !=null){
response.append(responseLIne.trim());
}
log.info("response.toString"+response.toString());
resultMsg=response.toString();
connection.disconnect();
}catch(IOException e){
e.printStackTrace();
}
return resultMsg;
}
//参数设置
String apiUrlCreate="http://174.3.2.2.:9890/aserverc/createChannel";
String jsonInputStringCreate="[{\n \"name\":\"小李\",\n \"sex\":\"女\"\n}]";
String apiUrlSend="http://174.3.2.2.:9890/aserverc/sendMessage";
String payloadSend="{\n \"name\": \"test\"\n}";
//创建 线程1并启动
Thread thread1=new Thread(new Runnable(){
public void run(){
sendPostCreateChannel(apiUrlCreate,jsonInputStringCreate);
}
});
thread1.start();//开启线程
Thread.sleep(5000); 让主线程睡眠5妙为了等线程1请求
//创建线程2并启动
Thread thread2=new Thread(new Runnable(){
public void run(){
String regmsg=sendPostMessage(apiUrlSend,payloadSend);
log.info("regmsg=="+regmsg);
vars.put("regmsg",regmsg);//为了后面断言使用
}
});
Thread2.start();//开启线程2
thread1.join();
thread2.join();主线程等待每个线程执行完
这是两个接口CASE写在同一个脚本里,也可以创建连接的接口写beanshell.发送的接口引用Case
前置的创建连接写到创建第一个线程加等待执行第二个发送消息的case
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//创建连接
private static String sendPostCreateChannel(String apiUrl,String jsonInputString){
String responsecreate="";
try{
URL url=new URL(apiUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方法为POST
connection.setRequestMethod("POST");
//设置请求头
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","*/*");
connection.setRequestProperty("App_Id","KC_ICS");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("ClientId","dddd");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Host","173.9.9.3:9089");
connection.setRequestProperty("Postman-Token","erer");
//启用输入输出流
connection.setDoOutput(true);
//写入JSON数据
OutputStream os=connection.getOutputStream();
byte[] input=jsonInputString.getBytes("utf-8");
os.write(input,0,input.length);
//获取返回的数据
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder response=new StringBuilder();
String responseLIne;
while((responseLine=br.readLine()) !=null){
response.append(responseLIne.trim());
}
log.info("response.toString"+response.toString());
responsecreate=response.toString();
connection.disconnect();
}catch(IOException e){
e.printStackTrace();
}
return responsecreate
}
//创建连接
private static String sendPostSendMessage(String apiUrl,String jsonInputString){
String resultMsg="";
try{
URL url=new URL(apiUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方法为POST
connection.setRequestMethod("POST");
//设置请求头
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","*/*");
connection.setRequestProperty("App_Id","KC_ICS");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("ClientId","dddd");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Host","localhost:8081");
connection.setRequestProperty("Postman-Token","erer");
connection.setRequestProperty("Trand_Id","34343");
//启用输入输出流
connection.setDoOutput(true);
//写入JSON数据
OutputStream os=connection.getOutputStream();
byte[] input=jsonInputString.getBytes("utf-8");
os.write(input,0,input.length);
//获取返回的数据
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder response=new StringBuilder();
String responseLIne;
while((responseLine=br.readLine()) !=null){
response.append(responseLIne.trim());
}
log.info("response.toString"+response.toString());
resultMsg=response.toString();
connection.disconnect();
}catch(IOException e){
e.printStackTrace();
}
return resultMsg
}
//参数设置
String apiUrlCreate="http://174.3.2.2.:9890/aserverc/createChannel";
String jsonInputStringCreate="[{\n \"name\":\"小李\",\n \"sex\":\"女\"\n}]";
String apiUrlSend="http://174.3.2.2.:9890/aserverc/sendMessage";
String payloadSend="{\n \"name\": \"test\"\n}";
//创建 线程1并启动
Thread thread1=new Thread(new Runnable(){
public void run(){
sendPostCreateChannel(apiUrlCreate,jsonInputStringCreate);
}
});
thread1.start();//开启线程
Thread.sleep(5000); 让主线程睡眠5妙为了等线程1请求
最后等待线程结束thread1.join();