web_custom_request请求关联session的问题(二)

来源:互联网 发布:云豹直播系统源码安装 编辑:程序博客网 时间:2024/06/08 18:22

问题

loadrunner 脚本中关联session仍不可用

过程分析

//错误写法char *body;body=        "{"        "\"body\":{"            "\"scanCode\":\"xxxxxxxxxx\","            "\"longitude\":\"xxx\","            "\"latitude\":\"xxx\","            "\"openLock\":\"0\""        "},"        "\"header\":{"            "\"appType\":\"1\","            "\"appVersion\":\"2.1.2\","            "\"cardCode\":\"xxxxxxxxxx\","            "\"channelId\":\"xxxxxxxxxx\","            "\"cityId\":\"591\","            "\"deviceId\":\"xxxxxxxxxx\","            "\"sessionId\":{getSessionId}"
  • 将获得的getSessionId打印出来,直接替换到后面的请求{getSessionId}里发请求时遇到换行符返回登录失败
  • 将换行符转义(删除过滤掉发现也行)能发起正确的请求响应
  • 请求的数据是Json格式,含 {}符号,参数的获取也是{},是否存在冲突
  • 最后发现定义的变量里不能这么用参数,通过打印body数据发现打印的是字符串“{getSessionId}”,没有进行值替换

处理

  • 定义字符串变量,将body常量拼接
  • 将session值通过方法lr_eval_string(“{getSessionId}”)传入,然后再通过strcat拼接到body中
  • 过滤sessionId中的换行符(参考:LoadRunner中自定义C函数实现字符串替换)
// 没有初始化body,会得到不可预知的结果char body[1024]="";strcat(body,        "{"        "\"body\":{"            "\"scanCode\":\"xxxxxxxxxx\","            "\"longitude\":\"xxx\","            "\"latitude\":\"xxx\","            "\"openLock\":\"0\""        "},"        "\"header\":{"            "\"appType\":\"1\","            "\"appVersion\":\"2.1.2\","            "\"cardCode\":\"xxxxxxxxxx\","            "\"channelId\":\"xxxxxxxxxx\","            "\"cityId\":\"591\","            "\"deviceId\":\"xxxxxxxxxx\","            "\"sessionId\":");    strcat(body,lr_eval_string("{getSessionId}"));    strcat(body,"}}");

再次发起请求,得到了预期的响应。

原创粉丝点击