本文介绍了Jmeter-如何替换字符串并重新发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将从响应中删除URL并将其再次发送出去.使用正则表达式提取器,我成功获取了所需的URL,但其中包含&"所以自然而然地发出请求时失败了.例子:GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93- 111& po = 642& re = 1& lt = 0& cc = VN& acp =& pcp =]/

I'm trying to create a script that will take a URL out of a response and send it out again.Using the regular expression extractor I've succeeded in taking the wanted URL, but it holds "&" so naturally when sending it out the request fails.Example:GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93-111&po=642&re=1&lt=0&cc=VN&acp=&pcp=]/

我正在尝试替换&"带有&".我已经尝试过:$ {__ javaScript($ {url} .replace(&",&"))}}但这没有用.我也尝试过正则表达式功能-相同.我不确定请求中的IP字段是否支持功能.

I'm trying to replace the "&" with a "&".I've tried: ${__javaScript(${url}.replace("&","&"))}But it did not work. I've tried the regex function as well- the same.I'm not sure the IP field in the request supports the us e of functions.

我目前正在尝试使用beanshell后处理器.但是我很确定我缺少一个更简单的解决方案.

I'm currently trying to use the beanshell post-processor. But I'm pretty sure there is a simpler solution I'm missing.

推荐答案

不确定将&替换为&会得到什么,但是会尝试响应.

Not sure what you're trying to get by replacing & with & however will try to respond.

  1. 首先:给定多个&实例,您需要使用replaceall函数,而不是替换
  2. 第二个:replace/replaceall函数采用 RegEx 作为参数,因此您需要转义&
  3. 如果您想实时替换URL路径,则需要 Beanshell预处理器,而不是后处理器
  1. First of all: given multiple & instances you need to use replaceall function, not replace
  2. Second: replace / replaceall functions take a RegEx as parameter, so you'll need to escape your &
  3. If you're trying to substitute URL Path in realtime, you'll need Beanshell Pre Processor, not the Post Processor

示例Beanshell预处理程序代码

Sample Beanshell Pre-Processor code

import java.net.URL;

URL myURL = sampler.getUrl();
String path = myURL.getPath();
String path_replaced = path.replaceAll("\\&", "&");
vars.put("NEW_PATH", path_replaced);

之后,将${NEW_PATH}放入HTTP请求的路径:"部分.

After that put ${NEW_PATH} to "Path:" section of your HTTP Request.

希望这会有所帮助.

这篇关于Jmeter-如何替换字符串并重新发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:33