使用jmeter中java请求对http接口进行测试

idea中创建一个maven项目
在pom文件中添加2个依赖包


      org.apache.jmeter
      ApacheJMeter_core
      5.1.1
    

      org.apache.jmeter
      ApacheJMeter_java
      5.1.1
    

发起http请求,我这使用的是 httpclint,还有json包也需要再pom文件中添加、


      org.apache.httpcomponents
      httpclient
      4.5.6
    

      com.alibaba
      fastjson
      1.2.68
    

创建http请求工具类,这里就写了个post的

package utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.logging.Logger;

public class HttpRequest {
    Logger log = Logger.getLogger(String.valueOf(HttpRequest.class));
    public HttpResponse Request(String url, Object object) {
        HeaderUtil headerUtil=new HeaderUtil();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = null;
        try {
            StringEntity stringEntity = new StringEntity(object.toString());
//            log.info(">>>>>>>>>>>>>>>"+object.toString());
            httpPost.setEntity(stringEntity);
            httpPost.setHeaders(headerUtil.GetHeader());
            httpResponse = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return httpResponse;
    }
//对写的post请求进行测试,可以查看返回结果,我这边写的是查看返回StatusCode
    public static void main(String[] args) throws IOException {
        HttpRequest httpRequest=new HttpRequest();
        String url="http://xxxx:xxx/login";
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userName","xxx");
        jsonObject.put("password","xxxx");
        jsonObject.put("url","xxxx");
        HttpResponse httpResponse=httpRequest.Request(url,jsonObject);
        String entity= EntityUtils.toString(httpResponse.getEntity());
        System.out.println(">>>>>>>>>>"+httpResponse.getStatusLine().getStatusCode());
    }
}


http请求头部信息,这边也抽离出来了一个,方便后续扩展

package utils;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

public class HeaderUtil {
    public Header[] GetHeader(){
        Header[]header=new Header[2];
        header[0]=new BasicHeader("Content-Type","application/json");
        header[1]=new BasicHeader("Referer","xxxxxx");
        return header;
    }
}

新增一个类实现JavaSamplerClient

package Jmeter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import utils.HttpRequest;
import java.util.logging.Logger;

public class LoginTest implements JavaSamplerClient {
    Logger log = Logger.getLogger(String.valueOf(LoginTest.class));
    @Override
    public void setupTest(JavaSamplerContext javaSamplerContext) {

    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
//        赋予请求名称,jmeter结果树中展示的名称
        sampleResult.setSampleLabel("登录请求");
        HttpRequest httpRequest = new HttpRequest();
//        获取参数
        String InterfaceUrl = javaSamplerContext.getParameter("InterfaceUrl");
        String userName = javaSamplerContext.getParameter("userName");
        String password = javaSamplerContext.getParameter("password");
        String url = javaSamplerContext.getParameter("url");
//        组装参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userName", userName);
        jsonObject.put("password", password);
        jsonObject.put("url", url);
//        设置开始
        sampleResult.sampleStart();
        try {
//            发送post请求
            HttpResponse httpResponse = httpRequest.Request(InterfaceUrl, jsonObject);
//            获取响应结果
            String entity=EntityUtils.toString(httpResponse.getEntity());
            JSONObject jsonObject1=  JSON.parseObject(entity);
//            获取successful状态值,这边主要判断这个值是true,就代表是请求到了后端
            String status=  jsonObject1.get("successful").toString();
//            获取请求状态码
            int code =httpResponse.getStatusLine().getStatusCode();
            if (status.equals("true")&&code==200) {
//                当successful=true,响应码200设置请求为成功
                sampleResult.setSuccessful(true);
                sampleResult.setResponseData(entity, "utf-8");
                log.info(">>>>>>>>>>>>>>>>>>>>>>>"+entity);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(entity, "utf-8");

            }
        } catch (Exception e) {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }

    @Override
    public void teardownTest(JavaSamplerContext javaSamplerContext) {

    }

    @Override
    public Arguments getDefaultParameters() {
//        添加参数,并赋予默认值
        Arguments arguments = new Arguments();
        arguments.addArgument("InterfaceUrl","xxxxxxx");
        arguments.addArgument("userName", "xxx");
        arguments.addArgument("password", "xxx");
        arguments.addArgument("url", "xxxxx");
        return arguments;
    }
     //进行测试
    public static void main(String[] args) {
        LoginTest jmeter = new LoginTest();
        JavaSamplerContext javaSamplerContext = new JavaSamplerContext(jmeter.getDefaultParameters());
        SampleResult sampleResult=jmeter.runTest(javaSamplerContext);
        byte[] ResponseData = sampleResult.getResponseData();
//        byte转换成String
        String s = new String(ResponseData);
        System.out.println(s);

    }
}

使用jmeter中java请求对http接口进行测试_第1张图片
测试没问题了,就打成jar,放到apache-jmeter-5.1.1\lib\ext下
使用jmeter中java请求对http接口进行测试_第2张图片

jmeter中创建Java请求
使用jmeter中java请求对http接口进行测试_第3张图片
请求成功的
使用jmeter中java请求对http接口进行测试_第4张图片
请求失败的
使用jmeter中java请求对http接口进行测试_第5张图片

你可能感兴趣的:(使用jmeter中java请求对http接口进行测试)