新的項目中,需要向應用發送一個中文的人名: payerName : 張三,調用失敗。,觀察錯誤日志,發現 傳過去的payerName是亂碼。
解決過程:
1. Virtual User Gen的Tools->Recoding Options -> Advanced -> Support charset -> UTF-8
重試之,無效。。。
2. 使用lr_convert_string_encoding函數進行強制轉碼。
[cpp] view plaincopyprint?
lr_convert_string_encoding: 對中文進行UTF-8轉碼
int lr_convert_string_encoding ( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
該函數有4個參數,含義如下:
sourceString:被轉換的源字符串。
fromEncoding:轉換前的字符編碼。
toEncoding:要轉換成為的字符編碼。
paramName:轉換后的目標字符串。
lr_convert_string_encoding: 對中文進行UTF-8轉碼
int lr_convert_string_encoding ( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
該函數有4個參數,含義如下:
sourceString:被轉換的源字符串。
fromEncoding:轉換前的字符編碼。
toEncoding:要轉換成為的字符編碼。
paramName:轉換后的目標字符串。
注意: 使用這個函數轉碼出來的字符串會以 \x00 結尾,所以要做一次額外處理。
代碼如下:
[cpp] view plaincopyprint?
char tmp[50];
lr_convert_string_encoding("張三", LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str");
strcpy(tmp,lr_eval_string("{str}"));
lr_save_string(tmp,"payerName");
char tmp[50];
lr_convert_string_encoding("張三", LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str");
strcpy(tmp,lr_eval_string("{str}"));
lr_save_string(tmp,"payerName");
然后再使用 如下方式進行調用:
[cpp] view plaincopyprint?
web_custom_request("consume",
"URL=http://192.168.12.89:8010/quickpay/v10/003",
"Method=POST",
"Resource=0",
"RecContentType=application/json",
"Referer=",
"Mode=HTTP",
"EncType=application/json",
"Body={\"cardHolderName\":\"{payerName}\",\"cardNo\":\"4392260802828457\",\"cardTypeEnum\":\"CREDI\"}",
LAST);
web_custom_request("consume",
"URL=http://192.168.12.89:8010/quickpay/v10/003",
"Method=POST",
"Resource=0",
"RecContentType=application/json",
"Referer=",
"Mode=HTTP",
"EncType=application/json",
"Body={\"cardHolderName\":\"{payerName}\",\"cardNo\":\"4392260802828457\",\"cardTypeEnum\":\"CREDI\"}",
LAST);
再次運行腳本,OK了。
原文轉自:http://blog.csdn.net/on_my_way20xx/article/details/9838163