Jmeter调用阿里大于短信接口
话不多说,直接贴代码
1 package cn.litry.sms; 2 3 import org.apache.jmeter.config.Arguments; 4 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 5 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 6 import org.apache.jmeter.samplers.SampleResult; 7 8 import com.taobao.api.ApiException; 9 import com.taobao.api.DefaultTaobaoClient; 10 import com.taobao.api.TaobaoClient; 11 import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; 12 import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; 13 14 public class JMeter extends AbstractJavaSamplerClient { 15 private String appkey;// 密钥 16 private String secret;// 密匙 17 private String Template;// 模板ID 18 private String Phone_No;// 手机号 19 private String ParamStr;// 参数 20 private String SignName;// 签名 21 22 public Arguments getDefaultParameters() { // 设置参数的默认值 23 Arguments def = new Arguments(); 24 def.addArgument("appkey", "密钥"); 25 def.addArgument("secret", "密匙"); 26 def.addArgument("Template", "模板ID"); 27 def.addArgument("Phone_No", "手机号"); 28 def.addArgument("ParamStr", "参数"); 29 def.addArgument("SignName", "签名"); 30 return def; 31 } 32 33 public void setupTest(JavaSamplerContext arg0) {// 每个线程测试前执行一次,做一些初始化工作。 34 appkey = arg0.getParameter("appkey"); 35 secret = arg0.getParameter("secret"); 36 Template = arg0.getParameter("Template"); 37 Phone_No = arg0.getParameter("Phone_No"); 38 ParamStr = arg0.getParameter("ParamStr"); 39 SignName = arg0.getParameter("SignName"); 40 } 41 42 public SampleResult runTest(JavaSamplerContext arg0) { 43 SampleResult sr = new SampleResult(); 44 sr.sampleStart();// 计时开始 45 String state = ""; 46 try {/*偷懒*/ 47 String url = "http://gw.api.taobao.com/router/rest"; 48 TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret); 49 AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest(); 50 req.setSmsType("normal"); 51 req.setSmsFreeSignName(SignName); 52 req.setSmsParamString(ParamStr); 53 req.setRecNum(Phone_No); 54 req.setSmsTemplateCode(Template); 55 AlibabaAliqinFcSmsNumSendResponse rsp; 56 rsp = client.execute(req); 57 state = rsp.getBody(); 58 sr.setResponseData("出参:" + state, null); 59 sr.setSuccessful(true); 60 } catch (ApiException e) { 61 sr.setSuccessful(false); 62 } 63 sr.sampleEnd();// 结束计时 64 return sr; 65 } 66 67 public void teardownTest(JavaSamplerContext arg0) {/* 测试结束时调用,只执行一次 */ 68 } 69 }
导出JAR到JMeert\lib\ext下:
运行Jmeter,添加线程组-->Sampler-->JAVA请求:(敏感信息参数化)
启动-->
N秒后:MyPhone收到短信:
【子正】JMeter接口调用成功!参数:OK!
未完待续...
TryEverything