在LoadRunner中,爲我們提供了一個字符串編碼轉換的函數
int lr_convert_string_encoding ( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
該函數有4個參數,含義如下:
sourceString:被轉換的源字符串。
fromEncoding:轉換前的字符編碼。
toEncoding:要轉換成爲的字符編碼。
paramName:轉換後的目標字符串。
在本例中可以看到,我們需要把字符編碼轉換爲UTF-8格式,因此用法如下:
lr_convert_string_encoding("汽車",LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str");
這樣一來,就成功地完成了字符串的編碼轉換。此時我們就可以對"汽車"這個參數進行參數化,參數化的方法很簡單,地球人都知道!於是最終的腳本編碼看起來像這樣:
lr_convert_string_encoding("lr_eval_string("{name}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str")
由於url不能傳輸漢字,所以程序要把漢字轉換一下,lr怎麼參數化呢
看下面的列子
web_url("0.00",
"URL=http://192.168.0.15/xxx/xxx/xxx.aspx?HotelName=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86",
"Resource=0",
"RecContentType=text/html",
"Referer=http://192.168.0.15/xxx/xxx/xxx.aspx?hotelid=00101210&hotelname=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86",
"Snapshot=t41.inf",
"Mode=HTTP",
LAST);
注意黑體字,被轉化了的漢字,使用如下函數
定義兩個變量
char temp;
char hotelname[4096];
lr_convert_string_encoding(lr_eval_string("{hotel_name}"),LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8 ,"temp");
轉化完的字符放到了temp裏,
strcat(hotelname,lr_eval_string ( "{temp}" ));
web_url("0.00",
"URL=http://192.168.0.15/xxx/xxx/xxx.aspx?HotelName={hotelname}",
"Resource=0",
"RecContentType=text/html",
"Referer=http://192.168.0.15/xxx/xxx/xxx.aspx?hotelid=00101210&hotelname=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86",
"Snapshot=t41.inf",
"Mode=HTTP",
LAST);