Loadruuner关于编码问题的处理

上一篇 / 下一篇  2010-01-12 20:36:33 / 个人分类:LOADRUNNER

URL编码和页面编码不一致导致了今天一整天都在研究这个东东,终于搞明白了,自己做个笔记。
此函数web_convert_param("to_url", "SourceEncoding=HTML", "TargetEncoding=URL", LAST);
//web_convert_param这个函数完成url encode功能。

intweb_convert_param(const char *ParamName,[char*SourceString] char*SourceEncoding, char *TargetEncoding, LAST);

 


Part
Description
ParamName
The name of a parameter. The converted string will be saved in this parameter. IfSourceStringis not used, it initially contains the text to convert. Note that this is not a name–value pair. Pass only the name of the parameter.
SourceString
A literal string to convert. "SourceString=your text"
SourceEncoding
The encoding type of the source data: HTML or plain text, in the format:"SourceEncoding=HTML
or
"SourceEncoding=PLAIN"
TargetEncoding
The target data format: URL or plain text, in the format"TargetEncoding=URL"
or
"TargetEncoding=PLAIN"
LAST
A marker indicating the end of the argument list.

  

Theweb_convert_paramfunction either converts HTML text to plain text or URL, or converts plain text to URL.

IfSourceStringis not passed, the text in parameterParamNameis converted and the result overwrites the parameter value. IfSourceStringis passed, the source string is converted and stored in parameterParamName.

SourceStringcan contain parameters. The source string is first evaluated, replacing parameters with their values, then converted. The result is stored in parameterParamName.

HTML format uses codes for some non–alphanumerical characters, such as&for ampersand. URLs do not support non–alpha–numerical characters and use the escape sequence %numberto represent them. To use an HTML value in a URL, you must therefore convert it into an escape sequence or plain text. For example, an ampersand is represented in a URL as%26. When you use this function to convert the HTML to plain text, it converts it to the way the character would appear in a browser.

The following table shows the same content in HTML, URL, and plain text formats:


HTML
URL
Plain Text
<mytag>&
%3Cmytag%3E%26
<mytag>&

This function is useful when you retrieve a URL from an HTML document, and you want to use it in an HTTP request such asweb_custom_request.



此函数lr_convert_string_encoding实现编码转换。
sourceString
The string to convert
fromEncoding
The encoding of the sourceString
toEncoding
The encoding to convert of the string saved in parameterparamName
paramName
The name of the parameter in which the destination string will be saved

lr_convert_string_encodingconverts a string encoding between the following encodings: System locale, Unicode, and UTF-8. The function saves the result string, including its terminating NULL, in the parameterparamName.

lr_convert_string_encodingis added manually to a script. when needed. It is not recorded.

Possible values for 'fromEncoding' and 'toEncoding' :


Constant
Value
LR_ENC_SYSTEM_LOCALE
NULL
LR_ENC_UTF8
"utf-8"
LR_ENC_UNICODE
"ucs-2"

51Testing软件测试网0O0Z!}-d6KA

Ls0jvu+tb0

X5e4cH\|%d0

&L'byqer*UT/Z'DD0为了去掉这个最后的\x00,可以通过下面这个办法去掉
la%s7S)C e |0lr_save_string(lr_eval_string("{temp}"),"temp");51Testing软件测试网\M)hH TdK

.XV7?4EBd&YbH!J0当你对一个带有/x00的参数取值时,会自动去除掉,所以只需要再存一次就行了.

如下代码轻松实现编码转换:
lr_convert_string_encoding(lr_eval_string("{Location}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str0");
         lr_convert_string_encoding(lr_eval_string("{Allword}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str1");
         lr_convert_string_encoding(lr_eval_string("{Degree}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str2");
        lr_convert_string_encoding(lr_eval_string("{Type}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str3");
         lr_save_string(lr_eval_string("{str0}"),"str0");
         lr_save_string(lr_eval_string("{str1}"),"str1");
         lr_save_string(lr_eval_string("{str2}"),"str2");
         lr_save_string(lr_eval_string("{str3}"),"str3");
         web_convert_param("str0",
                           "SourceString={str0}","SourceEncoding=PLAIN",
                           "TargetEncoding=URL", LAST);
         web_convert_param("str1",
                           "SourceString={str1}","SourceEncoding=PLAIN",
                           "TargetEncoding=URL", LAST);
         web_convert_param("str2",
                           "SourceString={str2}","SourceEncoding=PLAIN",
                           "TargetEncoding=URL", LAST);
        web_convert_param("str3",
                           "SourceString={str3}","SourceEncoding=PLAIN",
                           "TargetEncoding=URL", LAST);
            strcpy(tmp0,lr_eval_string("{str0}")); 
         strcpy(tmp1,lr_eval_string("{str1}"));
         strcpy(tmp2,lr_eval_string("{str2}"));
         strcpy(tmp3,lr_eval_string("{str3}"));
         sprintf(string,"URL=http://192.168.1.222/Jobs/List/l-%s/qa-%s/d-%s/p-7D/e-[1,3]/ty-%s",tmp0,tmp1,tmp2,tmp3);
         lr_output_message ("%s",string);
日志为:Action.c(360): Notify: Parameter Substitution: parameter "Location" =  "深圳"
Action.c(360): Notify: Saving Parameter "str0 = 娣卞湷\x00"
Action.c(361): Notify: Parameter Substitution: parameter "Allword" =  "软件"
Action.c(361): Notify: Saving Parameter "str1 = 杞欢\x00"
Action.c(362): Notify: Parameter Substitution: parameter "Degree" =  "本科"
Action.c(362): Notify: Saving Parameter "str2 = 鏈\x00"
Action.c(363): Notify: Parameter Substitution: parameter "Type" =  "全职"
Action.c(363): Notify: Saving Parameter "str3 = 鍏ㄨ亴\x00"
Action.c(364): Notify: Parameter Substitution: parameter "str0" =  "娣卞湷\x00"
Action.c(364): Notify: Saving Parameter "str0 = 娣卞湷"
Action.c(365): Notify: Parameter Substitution: parameter "str1" =  "杞欢\x00"
Action.c(365): Notify: Saving Parameter "str1 = 杞欢"
Action.c(366): Notify: Parameter Substitution: parameter "str2" =  "鏈\x00"
Action.c(366): Notify: Saving Parameter "str2 = 鏈"
Action.c(367): Notify: Parameter Substitution: parameter "str3" =  "鍏ㄨ亴\x00"
Action.c(367): Notify: Saving Parameter "str3 = 鍏ㄨ亴"
Action.c(368): Notify: Parameter Substitution: parameter "str0" =  "娣卞湷"
Action.c(368): Notify: Saving Parameter "str0 = %E6%B7%B1%E5%9C%B3"
Action.c(368): web_convert_param was successful      [MsgId: MMSG-26392]
Action.c(371): Notify: Parameter Substitution: parameter "str1" =  "杞欢"
Action.c(371): Notify: Saving Parameter "str1 = %E8%BD%AF%E4%BB%B6"
Action.c(371): web_convert_param was successful      [MsgId: MMSG-26392]
Action.c(374): Notify: Parameter Substitution: parameter "str2" =  "鏈"
Action.c(374): Notify: Saving Parameter "str2 = %E6%9C%AC%E7%A7%91"
Action.c(374): web_convert_param was successful      [MsgId: MMSG-26392]
Action.c(377): Notify: Parameter Substitution: parameter "str3" =  "鍏ㄨ亴"
Action.c(377): Notify: Saving Parameter "str3 = %E5%85%A8%E8%81%8C"
Action.c(377): web_convert_param was successful      [MsgId: MMSG-26392]
Action.c(380): Notify: Parameter Substitution: parameter "str0" =  "%E6%B7%B1%E5%9C%B3"
Action.c(381): Notify: Parameter Substitution: parameter "str1" =  "%E8%BD%AF%E4%BB%B6"
Action.c(382): Notify: Parameter Substitution: parameter "str2" =  "%E6%9C%AC%E7%A7%91"
Action.c(383): Notify: Parameter Substitution: parameter "str3" =  "%E5%85%A8%E8%81%8C"
Action.c(385): URL=http://192.168.1.222/Jobs/List/l-%E6%B7%B1%E5%9C%B3/qa-%E8%BD%AF%E4%BB%B6/d-%E6%9C%AC%E7%A7%91/p-7D/e-[1,3]/ty-%E5%85%A8%E8%81%8C

时间紧,该下班了,有空再整理。欢迎光临!

TAG:

 

评分:0

我来说两句

Open Toolbar