1. 程式人生 > 其它 >Jmeter二次開發 java取樣器示例

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");

本文來自部落格園,作者:

up~up,轉載請註明原文連結:https://www.cnblogs.com/soft-engineer/p/14985177.html