1. 程式人生 > >loadrunner介面請求後返回中文亂碼入坑

loadrunner介面請求後返回中文亂碼入坑

情景:在練習過程中,模擬請求一個介面,在返回中,中文亂碼

程式碼如下

	web_reg_save_param("response",
					   "LB={",
					   "RB=}",
					   "Search=All",
					   "Ord=All",
		LAST);
	web_url("Hit the Slopes and Save", 

    "URL=http://api.k780.com:88/?app=life.postcode&appkey=38067&sign=2b8b7baa13bd02f61399f87fd0ae5a0e&postcode=528400"
, "TargetFrame=Main", "Resource=0", "Mode=HTML", LAST ); // 轉換之前輸出 lr_output_message("%s",lr_paramarr_random("response")); // 手動轉換後輸出 lr_convert_string_encoding(lr_paramarr_random("response"),"utf-8",NULL,"msg"); lr_output_message("%s",lr_eval_string("{msg}"));

執行結果:

Starting iteration 1.
Starting action Action7. Action7.c(3): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action7.c(9): Notify: Saving Parameter "response_1 = "areaid":"242","postcode":"528400","areacode":"0760","areanm":"涓崕浜烘皯鍏卞拰鍥\xbd,騫誇笢鐪\x81,涓北甯\x82","simcall":"涓浗,騫誇笢,涓北"". Action7.c(9): HTML parsing not performed for
Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://api.k780.com:88/?app=life.postcode&appkey=38067&sign=2b8b7baa13bd02f61399f87fd0ae5a0e&postcode=528400" [MsgId: MMSG-26548] Action7.c(9): Notify: Saving Parameter "response_count = 1". Action7.c(9): web_url("Hit the Slopes and Save") was successful, 189 body bytes, 171 header bytes, 11 chunking overhead bytes [MsgId: MMSG-26385] Action7.c(21): Notify: Parameter Substitution: parameter "response_count" = "1" Action7.c(21): Notify: Parameter Substitution: parameter "response_1" = ""areaid":"242","postcode":"528400","areacode":"0760","areanm":"涓崕浜烘皯鍏卞拰鍥\xbd,騫誇笢鐪\x81,涓北甯\x82","simcall":"涓浗,騫誇笢,涓北"" Action7.c(21): "areaid":"242","postcode":"528400","areacode":"0760","areanm":"涓崕浜烘皯鍏卞拰鍥?騫誇笢鐪?涓北甯?,"simcall":"涓浗,騫誇笢,涓北" Action7.c(23): Notify: Parameter Substitution: parameter "response_count" = "1" Action7.c(23): Notify: Parameter Substitution: parameter "response_1" = ""areaid":"242","postcode":"528400","areacode":"0760","areanm":"涓崕浜烘皯鍏卞拰鍥\xbd,騫誇笢鐪\x81,涓北甯\x82","simcall":"涓浗,騫誇笢,涓北"" Action7.c(23): Notify: Saving Parameter "msg = "areaid":"242","postcode":"528400","areacode":"0760","areanm":"中華人民共和國,廣東省,中山市","simcall":"中國,廣東,中山"\x00". Action7.c(24): Notify: Parameter Substitution: parameter "msg" = ""areaid":"242","postcode":"528400","areacode":"0760","areanm":"中華人民共和國,廣東省,中山市","simcall":"中國,廣東,中山"\x00" Action7.c(24): "areaid":"242","postcode":"528400","areacode":"0760","areanm":"中華人民共和國,廣東省,中山市","simcall":"中國,廣東,中山" Ending action Action7. Ending iteration 1.

看下lr_convert_string_encoding 這個函式的用法

int lr_convert_string_encoding( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName); 
  • sourceString The string to convert
  • fromEncoding The encoding of the sourceString
  • toEncoding The encoding to convert of the string saved in parameter paramName
  • paramName The name of the parameter in which the destination string will be saved

lr_convert_string_encoding converts 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 parameter paramName.

Printable characters in the result string are displayed in VuGen and its logs as the actual characters. Non-printable characters are displayed in Hexadecimal. For example:

rc = lr_convert_string_encoding("A", NULL, LR_ENC_UTF8, "stringInUnicode");

The result string displays as A\x00, and not \x41\x00, because the letter A is a printable character.

Add lr_convert_string_encoding 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”