zoukankan      html  css  js  c++  java
  • loadrunner请求中有汉字 如何编码

    这是一个HTTP接口测试中经常会碰到的问题,目前的服务器采用的都是UTF-8编码方式,而我们的客户机Windows系统一般默认采用的编码方式是GBK,这正是我们采用录制方式的时候会发现许多中文乱码的原因。

    Loadrunner录制的时候可以通过在Virtual User Gen的Tools->Recoding Options -> Advanced -> Support charset -> UTF-8的设置规避(其实也只是部分规避),下面我们讨论在手写测试脚本时如何解决UTF-8转码的问题。

    实践一:在脚本中直接采用中文明文进行请求

      web_custom_request("web_custom_request",
      "URL=http://172.16.4.191/list?id=环球影院",
      "Method=GET",
      "TargetFrame=",
      "Resource=0",
      "Referer=",
      "Body=",
      LAST);
    结果:服务端返回404错误,找不到相应的资源id,明显服务端不能正确响应非UTF8编码方式的请求。

    实践二:

    为解决这个问题,最关键的是要把本地GBK编码的汉字转换成UTF-8编码格式的信息,为此我们引进loadrunner自带的编码函数lr_convert_string_encoding

     

    lr_convert_string_encoding

     
     

    Converts a string to a different encoding.

    C Language

    intlr_convert_string_encoding( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);

    Example       See Also

     
     
    sourceString
    The string to convert
    fromEncoding
    The encoding of the sourceString
    toEncoding
    The encoding to convert of the string saved in parameterparamName
    paramName
    The name of the parameter in which the destination string will be saved
     

    lr_convert_string_encodingconverts 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 parameterparamName.

    lr_convert_string_encodingis added 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"
     
     
    根据函数说明,我们编写测试脚本如下
     lr_convert_string_encoding( "环球影院",
      LR_ENC_SYSTEM_LOCALE,
      LR_ENC_UTF8,
      "str" );
     web_custom_request("web_custom_request",
      "URL=http://172.16.4.191/list?id={str}",
      "Method=GET",
      "TargetFrame=",
      "Resource=0",
      "Referer=",
      "EncType=text/xml;charset=UTF-8",
      "Body=",
      LAST);
     
    使用lr_convert_string_encoding函数,将中文转换成UTF-8编码以后,作为参数传递给请求,并发送。
    测试结果:仍然返回404错误,查看loadrunner日志信息“环球影院”已经正确转换成UTF8编码方式,那为什么还是请求失败呢?
    再次查看日志如下
    Action.c(7): t=825ms: 223-byte request headers for "http://172.16.4.191/list?id=鐜�悆褰遍櫌" (RelFrameId=1)
    Action.c(7):     GET /list?id=鐜�悆褰遍櫌x00 HTTP/1.1
    Action.c(7):     Content-Type: text/xml;charset=UTF-8
    Action.c(7):     User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows)
    Action.c(7):     Accept-Encoding: gzip, deflate
    Action.c(7):     Accept: */*
    Action.c(7):     Connection: Keep-Alive
    Action.c(7):     Host: 172.16.4.191
    Action.c(7):    
    发现在请求地址“/list?id=鐜�悆褰遍櫌”后面还带了一个x00,这正是lr_convert_string_encoding函数说明中标红的说明:The function saves the result string, including its terminating NULL, in the parameterparamName.
    也就是说,我转换成UTF-8之后,如果直接作为变量传到代码之中的话,在最后的字符串之中,会多出来一个“NULL”,C语言中NULL是一个字符串的结束,而正是这个null字节的存在导致了服务端识别id出错。
     
    实践三:
     char tmp[50];
     lr_convert_string_encoding( "环球影院",
      LR_ENC_SYSTEM_LOCALE,
      LR_ENC_UTF8,
      "str" );  
     strcpy(tmp,lr_eval_string("{str}"));
    lr_log_message("str is %s",tmp);
     lr_save_string(tmp,"sorvalue");
     web_custom_request("web_custom_request",
      "URL=http://172.16.4.191/list?id={sorvalue}",
      "Method=GET",
      "TargetFrame=",
      "Resource=0",
      "Referer=",
      "Body=",
      LAST);
    通过lr_eval_string函数取参数值时会自动去掉x00,测试结果正常,正确返回HTTP响应内容。
  • 相关阅读:
    Firemonkey 控件设定字型属性及颜色
    ListView 使用 LiveBindings 显示超过 200 条记录
    Firemonkey ListView 获取项目右方「>」(Accessory) 事件
    XE7 Update 1 选 iOS 8.1 SDK 发布 iPhone 3GS 实机测试
    Firemonkey Bitmap 设定像素颜色 Pixel
    Firemonkey 移动平台 Form 显示使用 ShowModal 范例
    XE7 提交 App(iOS 8)提示「does not contain the correct beta entitlement」问题修复
    XE7 Android 中使用 MessageDlg 范例
    导出 XE6 预设 Android Style (*.style) 档案
    修正 Memo 設定為 ReadOnly 後, 無法有複製的功能
  • 原文地址:https://www.cnblogs.com/Darrenblog/p/9088618.html
Copyright © 2011-2022 走看看