[英]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
有不同的方法,例如:
將您的代碼存儲到文件中並使用__FileToString() function 從文件系統中讀取它
將代碼轉換為Base64 格式,它將產生如下一行:
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsgCgppbnQgbWFpbigpIAp7IAovL3VzZSBjaW4gdG8gZ2V0IGNvbnNvbGUgaW5wdXQgCnN0cmluZyB0ZXN0OwpnZXRsaW5lKGNpbix0ZXN0KTsKCnZlY3RvcjxjaGFyPiBuZXdzdHI7CmZvciAoaW50IGkgPSAwOyBpPCB0ZXN0LnNpemUoKTtpKyspCnsKY2hhciBjID0gdGVzdFtpXTsKaWYgKChjPj0nYScmJiBjIDw9J3onKSB8fCAoYz49J0EnJiYgYyA8PSdaJykpCnsKICAgIC8vY291dDw8ImM6ICI8PGM8PGVuZGw7CiAgICBuZXdzdHIucHVzaF9iYWNrKHRvbG93ZXIoYykpOwp9Cn0KCmlmIChuZXdzdHIuc2l6ZSgpPT0wKQp7CiAgICBjb3V0PDwiWUVTIjw8ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KZm9yIChpbnQgaSA9IDA7IGk8IG5ld3N0ci5zaXplKCkvMjtpKyspCnsKY2hhciBhID0gbmV3c3RyW2ldOwpjaGFyIGIgPSBuZXdzdHJbbmV3c3RyLnNpemUoKS0xLWldOwppZiAoYSAhPSBiKQp7CiAgICAvL2NvdXQ8PG5ld3N0cltpXTw8ZW5kbDsKICAgIGNvdXQ8PCJOTyI8PGVuZGw7CiAgICByZXR1cm4gMDsKfQoKfQpjb3V0PDwiWUVTIjw8ZW5kbDsgCgovL3VzZSBjb3V0IHRvIHByaW50IGNvbnNvbGUgb3V0cHV0IAoKcmV0dXJuIDA7IAp9
並使用__base64Decode() function將其轉換回 HTTP 請求采樣器參數部分
查看Apache JMeter 函數 - 介紹文章以了解有關 JMeter 函數概念的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.