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