首页 > 解决方案 > 请求登录 JMeter

问题描述

我需要发送一个 sha256 十六进制请求签名以及 http 请求。

从其他问题和答案中,有人建议使用 bean shell 预处理器,但这似乎不再起作用。

还有其他方法可以做到这一点吗?

这是我在 bean shell 预处理器中的代码

import org.apache.commons.codec.digest.DigestUtils;

String api_key = "";
String shared_secret = "";
long timestamp = System.currentTimeMillis()/1000;

String sig = DigestUtils.md5(api_key + shared_secret + timestamp);

vars.put("sig", sig);

log.info("Signature: " + sig);

这就是错误

2020-07-23 10:18:55,694 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35yNeSe37 . . . '' : Typed variable declaration
2020-07-23 10:18:55,694 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35 . . . '' : Typed variable declaration
2020-07

标签: javajmeter

解决方案


  1. 如果您说需要“sha256 hex”,为什么要使用DigestUtils.md5() ?
  2. 如果从 JMeter 3.1 开始您应该使用 Groovy,为什么要使用 Beanshell ?

我认为您应该将代码更改为:

vars.put("sig", org.apache.commons.codec.digest.DigestUtils.sha256Hex(api_key + shared_secret + timestamp))

有关 JMeter 中 Groovy 脚本的更多信息,请参阅Apache Groovy - 为什么以及如何使用它一文。

顺便说一句,有__digest() 函数可以让你的生活更轻松:

在此处输入图像描述


推荐阅读