如何使用BeanShell解码URL?

Del*_*ria 0 regex performance jmeter beanshell performance-testing

我需要在请求中传递一个参数(URL的一部分),例如:

"S;4;163;1;O;rAghjgjU="
Run Code Online (Sandbox Code Playgroud)

但是这个值被提取为一个正则表达式:

"S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D"
Run Code Online (Sandbox Code Playgroud)

我想使用BeanShell来解码URL.

Dmi*_*i T 6

  1. JMeter提供__urldecode()函数,所以如果你使用它:

    ${__urldecode(S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D)}
    
    Run Code Online (Sandbox Code Playgroud)

    你会得到所需的价值.

  2. 如果您仍想使用Beanshell,可以调用URLDecoder.decode(); 方法如:

    String decoded = URLDecoder.decode("S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D", "UTF-8");
    
    Run Code Online (Sandbox Code Playgroud)
  3. 还有__Beanshell()函数允许执行任意Beanshell脚本,调用它如:

    ${__BeanShell(URLDecoder.decode("S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D"\, "UTF-8"),)}
    
    Run Code Online (Sandbox Code Playgroud)

    有关JMeter中 Beanshell脚本的更多信息,请参阅如何使用BeanShell:JMeter的最爱内置组件,尤其要注意如何在Beanshell脚本中调用Java和JMeter API类.