Jmeter beanshell 遇到“;”

Jmeter beanshell Encounter ";"

我正在尝试在 jmeter 中为 URL 参数值执行 beanshell 脚本。我有以下内容:

${__BeanShell(vars.get("query").replaceAll(" ","%20"))}

jmeter 控制台输出如下:

Caused by: bsh.ParseException: In file: inline evaluation of: ``vars.get("query").replaceAll(" ";'' Encountered ";" at line 1, column 33.

我不知道问题出在哪里,因为字符是 , 而不是 ;

请在 Beanshell PreProcessor or BeanShell PostProcessor 中使用以下代码,以便将单个 space 字符替换为 '%20':

String myString = vars.get("query");
String new_var = myString.replaceAll(" ", "%20");
vars.put("updated_value", new_var);

您可以进一步使用 'updated_value' 变量,在下一个请求中将 space 替换为 '%20'

有关 JMeter 元素的更多信息,请参阅 JMeter Knowledge Base

参见JMetrer's functions教程,您需要转义每个逗号:

If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter.

你的情况

  ${__BeanShell(vars.get("query").replaceAll(" "\,"%20"))}

还可以考虑使用 __groovy 函数代替 __BeanShell 以获得更好的性能。

你在做一些荒谬的事情。编码 URL 不仅仅是关于转义空格,查看 URLEncoder 您需要处理的文档:

  • 所有非拉丁非字母数字字符
  • .-*_
  • 之外的所有字符

这可能非常棘手。

所以你基本上有两个选择:

在高负载情况下的性能方面 Groovy 是首选,查看 Apache Groovy - Why and How You Should Use It 文章了解更多详情。