LR实现普通文字转换为URL格式编码
来源:互联网 发布:淘宝运营主管岗位职责 编辑:程序博客网 时间:2024/05/21 19:45
http://motevich.blogspot.com/2007/09/loadrunner-convert-text-to-url-format.html
LoadRunner - how to convert a plain text to URLformat
The task - How to convert a plain text string to URL-format in LoadRunner?
Several days ago I faced with a simple task - it was needed to convert a passedparameter (which was in a plain text form) to URL-format. In other words, I hadto convert:
- string "ab" to the same string"ab"
- string "a b" to the string"a%20b"
- string "a b_c%" to"a%20b%5Fc%25"
- and so on ...
The cause - Why it was needed?
LoadRunner script contained parameters, which should be passed to URL directly.Some my parameters contained special characters like spaces (" "),quotes ("), percentage ("%"), asterisks ("*") andothers. To be processed correctly by a browser and a web server, thesecharacters should be translated and passed as their hex-codes.
For example:
- a space character (" ") has hex-code 0x20, so it should be passed as"%20"
- underline character (" ") has hex-code 0x5F, so it should be passed as"%5F"
The ways on how to perform the task.
To perform this task I decided to use LoadRunner's web_convert_param function. Ididn't know at that time that this function does not satisfy my requirements.So, I was reluctant to write my own function EncodePlainToURL. Well, let'ssee both solutions.
- web_convert_param function.
As Help says, web_convert_param function converts HTML to a URL or plain text.
So, I wrote this code:
char sIn[] ="t es%d$ +eprst_";
lr_save_string(sIn,"InputParam");
web_convert_param("InputParam", "SourceEncoding=PLAIN",
"TargetEncoding=URL", LAST);
lr_output_message("%s",lr_eval_string("{InputParam}"));
Press F5 toexecute the code, and.... H'm....
The input string "t es%d$ + eprst_" was converted to"t+es%25d%24+%2B+eprst_".
That means that space (" ") was converted to a plus("+"). I expected to see "%20" instead of a plus character.
Actually, it seems that "+" and "%20" are twins. Forexample Google uses a plus to encode a space within a search query. Forexample, if the query is "some text", then the URL will be:http://www.google.com/search?hl=en&q=some+text&btnG=Google+Search
I was tried to encode spaces (and others special characters) to theirhex-codes.
That's why the following function was written:
- EncodePlainToURL function.
The logic is simple enough:- If the current character is a digits or an alphabetic letter, then pass it "as is"
- Otherwise the current character is a special one. So, hex-code should be written in this case
This is a codeof EncodePlainToURL function:
/*
* EncodePlainToURL converts aplain text string to an URL-form string.
*
* Parameters: sIn - input string to be encoded to URL format
* sOut - outputbuffer
* Note: the size of"sOut" parameter should be at least equal to triple size
* of "sIn"parameter plus one character(for end-terminator '\0')
*
* Author: Dmitry Motevich
*
* Examples: "a" -> "a"
* "a b" -> "a%20b"
* "ab_cc:\/c%" -> "a%20b%5Fcc%3A%2Fc%25"
*/
char*EncodePlainToURL(constchar*sIn,char*sOut)
{
int i;
char cCurChar;
char sCurStr[4] = {0};
sOut[0] ='\0';
for(i=0;cCurChar= sIn[i];i++)
{
//if this is a digit or an alphabetic letter
if(isdigit(cCurChar) || isalpha(cCurChar))
{
//then write the current character "as is"
sprintf(sCurStr,"%c",cCurChar);
}
else
{
//else convert it to hex-form. "_" -> "%5F"
sprintf(sCurStr,"%%%X",cCurChar);
}
//append current item to the output string
strcat(sOut,sCurStr);
}
return sOut;
}
The example of usage is:
char sIn[] ="t es%d$ + eprst_";
char sOut[100];
lr_output_message("%s", EncodePlainToURL(sIn,sOut));
Execute thecode, and see the result:
The input string "t es%d$ + eprst_" was converted to"t%20es%25d%24%20%2B%20eprst%5F".
Yeah! :)
All special characters (including spaces) were converted to hex-code, i.e. toURL-format!
- LR实现普通文字转换为URL格式编码
- 文字转换为二进制/十六进制(Uncode编码 java实现)
- 编码格式转换--url编码与解码
- 编码格式转换--url编码与解码
- 将文字转换为encodeURIComponent格式
- 批量将文字转换为encoding格式
- 文本转换为HTML文字编码
- 将普通字符串转换成MD5编码格式字符
- Unity项目脚本编码格式转换为UTF8编码格式
- Android实现OCR文字识别并且转换为Excel、PDF格式输出
- js把时间戳转换为普通日期格式
- 通过libiconv实现文字编码转换
- 普通dos编码文件批量转换为UTF-8格式文件
- JS实现URL编码转换中文
- 将utf8等编码格式转换为shift_jis格式
- 将utf8等编码格式转换为shift_jis格式
- 把ArrayList转化为Json格式,在用url编码
- 了解文字编码格式
- 实习一个月总结
- /etc/sysctl.conf 参数说明
- 超酷概念版iPod Nano
- ASM文件拷贝到本地
- ExecuteNonQuery方法和ExecuteScalar方法的区别
- LR实现普通文字转换为URL格式编码
- oracle dbtimezone与os时区不一致的解决办法
- 如果男生是好的
- Spring(1)——文件资源操作
- C# 遍历FTP文件夹/下载
- 如何更改NUMBER类型为指定的大小
- 让你渐渐放弃了频繁联系曾经形影不离的人
- android service
- oracle 时间字段自动更新问题