Jmeter java取样器示例

public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

    //生成sampleResult对象,用于请求的命名、标记状态、添加返回内容等

    SampleResult sampleResult=new SampleResult();

    sampleResult.setSampleLabel("cctester_login");

    //调用上文中实现的post请求

    PostTest postTest=new PostTest();

    //接受JMeter界面上传输的参数

    String username = javaSamplerContext.getParameter("username");

    String password = javaSamplerContext.getParameter("password");

    String url = javaSamplerContext.getParameter("url");

    //标记请求开始

    sampleResult.sampleStart();

    try {

        HttpResponse result =postTest.Request(url,username,password);

        String entity= EntityUtils.toString(result.getEntity());

        //根据返回内容判断结果状态并展示结果

        if (result.getStatusLine().getStatusCode()==200){

            sampleResult.setSuccessful(true);

            sampleResult.setResponseCodeOK();

            sampleResult.setResponseData(entity, "utf-8");

        }else {

            sampleResult.setSuccessful(false);

            sampleResult.setResponseData(entity, "utf-8");

  )

 

https://www.cnblogs.com/shouke/p/10157481.html

posted @ 2021-07-08 11:07  up~up  阅读(125)  评论(0编辑  收藏  举报