繁体   English   中英

如何在 Jmeter 参数中保留换行符和空格

[英]How to preserve newlines and whitespaces in Jmeter parameters

在此处输入图像描述 嗨,我需要为我的请求向我的 Jmeter 参数添加一段代码,如下所示:

#include <string>
#include <vector>
#include <iostream>

using namespace std; 

int main() 
{ 
//use cin to get console input 
string test;
getline(cin,test);

vector<char> newstr;
for (int i = 0; i< test.size();i++)
{
char c = test[i];
if ((c>='a'&& c <='z') || (c>='A'&& c <='Z'))
{
    //cout<<"c: "<<c<<endl;
    newstr.push_back(tolower(c));
}
}

if (newstr.size()==0)
{
    cout<<"YES"<<endl;
        return 0;
    }
for (int i = 0; i< newstr.size()/2;i++)
{
char a = newstr[i];
char b = newstr[newstr.size()-1-i];
if (a != b)
{
    //cout<<newstr[i]<<endl;
    cout<<"NO"<<endl;
    return 0;
}

}
cout<<"YES"<<endl; 

//use cout to print console output 

return 0; 
}

但是,当我将它作为参数添加时,它会将整个代码放在一行中并且从不执行它。 即它采取如下方式:

#include <string> #include <vector> #include <iostream>  using namespace std;   int main()  {  //use cin to get console input  string test; getline(cin,test);  vector<char> newstr; for (int i = 0; i< test.size();i++) { char c = test[i]; if ((c>='a'&& c <='z') || (c>='A'&& c <='Z')) {    //cout<<"c: "<<c<<endl;     newstr.push_back(tolower(c)); } }  if (newstr.size()==0) {  cout<<"YES"<<endl;      return 0;   } for (int i = 0; i< newstr.size()/2;i++) { char a = newstr[i]; char b = newstr[newstr.size()-1-i]; if (a != b) {   //cout<<newstr[i]<<endl;    cout<<"NO"<<endl;   return 0; }  } cout<<"YES"<<endl;   //use cout to print console output   return 0;  } 

并且从不执行代码。

此外,代码在 postman 中工作正常,因为它接受空格和换行符。

我尝试使用转义序列,但它对我不起作用 有什么出路吗?

参数如下:

requestid:2
store:${hdn_store}
txtsol: #include <string>
#include <vector>
#include <iostream>

using namespace std; 

int main() 
{ 
//use cin to get console input 
string test;
getline(cin,test);

vector<char> newstr;
for (int i = 0; i< test.size();i++)
{
char c = test[i];
if ((c>='a'&& c <='z') || (c>='A'&& c <='Z'))
{
    //cout<<"c: "<<c<<endl;
    newstr.push_back(tolower(c));
}
}

if (newstr.size()==0)
{
    cout<<"YES"<<endl;
        return 0;
    }
for (int i = 0; i< newstr.size()/2;i++)
{
char a = newstr[i];
char b = newstr[newstr.size()-1-i];
if (a != b)
{
    //cout<<newstr[i]<<endl;
    cout<<"NO"<<endl;
    return 0;
}

}
cout<<"YES"<<endl; 

//use cout to print console output 

return 0; 
}

langcode:1

有不同的方法,例如:

  1. 将您的代码存储到文件中并使用__FileToString() function 从文件系统中读取它

  2. 将代码转换为Base64 格式,它将产生如下一行:

    I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsgCgppbnQgbWFpbigpIAp7IAovL3VzZSBjaW4gdG8gZ2V0IGNvbnNvbGUgaW5wdXQgCnN0cmluZyB0ZXN0OwpnZXRsaW5lKGNpbix0ZXN0KTsKCnZlY3RvcjxjaGFyPiBuZXdzdHI7CmZvciAoaW50IGkgPSAwOyBpPCB0ZXN0LnNpemUoKTtpKyspCnsKY2hhciBjID0gdGVzdFtpXTsKaWYgKChjPj0nYScmJiBjIDw9J3onKSB8fCAoYz49J0EnJiYgYyA8PSdaJykpCnsKICAgIC8vY291dDw8ImM6ICI8PGM8PGVuZGw7CiAgICBuZXdzdHIucHVzaF9iYWNrKHRvbG93ZXIoYykpOwp9Cn0KCmlmIChuZXdzdHIuc2l6ZSgpPT0wKQp7CiAgICBjb3V0PDwiWUVTIjw8ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KZm9yIChpbnQgaSA9IDA7IGk8IG5ld3N0ci5zaXplKCkvMjtpKyspCnsKY2hhciBhID0gbmV3c3RyW2ldOwpjaGFyIGIgPSBuZXdzdHJbbmV3c3RyLnNpemUoKS0xLWldOwppZiAoYSAhPSBiKQp7CiAgICAvL2NvdXQ8PG5ld3N0cltpXTw8ZW5kbDsKICAgIGNvdXQ8PCJOTyI8PGVuZGw7CiAgICByZXR1cm4gMDsKfQoKfQpjb3V0PDwiWUVTIjw8ZW5kbDsgCgovL3VzZSBjb3V0IHRvIHByaW50IGNvbnNvbGUgb3V0cHV0IAoKcmV0dXJuIDA7IAp9

并使用__base64Decode() function将其转换回 HTTP 请求采样器参数部分

查看Apache JMeter 函数 - 介绍文章以了解有关 JMeter 函数概念的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM