LoadRunner字符串编码转换函数:lr_convert_string_encoding

本文介绍了LoadRunner中的lr_convert_string_encoding函数,用于字符串编码转换。详细讲解了函数的参数及使用场景,特别是在处理URL请求中中文编码问题时的作用,帮助优化脚本调试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近在调试脚本的过程中,多次用到LoadRunner字符串编码转换函数lr_convert_string_encoding,这里简单总结一下,方便以后参考~

1.函数实现

Loadrunner自带函数,直接使用即可

lr_convert_string_encoding

//C Language

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


对应的中文参数解释:

   sourceString:被转换的源字符串。

   fromEncoding:转换前的字符编码。

   toEncoding:要转换成为的字符编码。

   paramName:转换后的目标字符串。

lr_convert_string_encoding converts a string encoding betweenthe following encodings: System locale, Unicode, and UTF-8. The function savesthe result string, including its terminating NULL, in the parameter paramName.

根据上面的说明,这个函数其实只是把字符串在三种编码格式中做转换,所以其实功能也是有限的,不过也已经可以适用到我们常见的一些场景了。

常量和值的对应关系如下:

Possible values for 'fromEncoding'and 'toEncoding' :

Constant

Value

LR_ENC_SYSTEM_LOCALE

NULL

LR_ENC_UTF8

"utf-8"

LR_ENC_UNICODE

"ucs-2"


2.Loadrunner中的使用

   在URL请求的返回消息中,我们有时候会遇到中文返回被编码的情况,不易识别,这时候就可以用编码转换函数,方便调试脚本。


Action()
{
	int compare=1;
	web_reg_save_param("payRespBody",
		"LB=",
		"RB=",
		"NotFound=ERROR",
		"Search=Body",
		LAST);

	web_reg_save_param("responseMsg",
		"LB=responseMsg\":\"",
		"RB=\",",
		"NotFound=ERROR",
		"Search=All",
		LAST);

	web_custom_request("pay.htm", 
		"URL=http://xxx/a/b/pay.htm?x={orderId}&pwd=x", 
		"Method=POST", 
		"TargetFrame=", 
		"Resource=0", 
		"RecContentType=application/json", 
		"Referer=", 
		"Mode=HTML", 
		"EncType=application/x-www-form-urlencoded; charset=UTF-8", 
		LAST);

	lr_convert_string_encoding(lr_eval_string("{responseMsg}"),"utf-8",NULL,"msg");//将返回消息responseMsg值做编码转换

	lr_output_message("转换编码前payRespBody----%s",lr_eval_string("{payRespBody}"));
	
	lr_convert_string_encoding(lr_eval_string("{payRespBody}"),"utf-8",NULL,"bodymsg"); <pre name="code" class="cpp">         //将返回消息的body做编码转换

	compare=strcmp(lr_eval_string("{msg}"),"支付成功");//将编码转换后的responseMsg值与可识别的中文做比较,判断事务成功或失败

	if(compare==0){

		lr_end_transaction("3--支付",LR_PASS);
			
		}
	else{
			lr_end_transaction("3--支付",LR_FAIL);
			
			lr_output_message("支付失败orderId:----%s,responseMsg----%s",lr_eval_string("{orderId}"),lr_eval_string("{msg}"));//打印转换编码后的返回

			lr_output_message("转换编码后payRespBody----%s",lr_eval_string("{bodymsg}"));//打印转换编码后的返回,便于定位问题
		}
		
	return 0;
}

通过对比转换前和转换后的值可以发现,编码转换后,中文变得可识别,方便了脚本调试和脚本问题定位及解决。
Action.c(x): 转换编码前payRespBody----{"responseMsg":"鏀粯澶辫触","success":false}
Action.c(y): 转换编码后payRespBody----{"responseMsg":"支付失败","success":false}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值