loadrunner脚本乱码时采用lr_convert_string_encoding

来源:互联网 发布:做淘宝仓库打包员累吗 编辑:程序博客网 时间:2024/05/17 12:25

在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);

原创粉丝点击