很多HTTP接口在传参时,需要先对接口的参数进行数据签名加密
如以下POST接口
http://localhost:8080/pinter/com/userInfo
参数为
{"phoneNum":"123434","optCode":"testfan","timestamp":"1211212","sign":"fdsfdsaafsasfas"}
其中,sign字段是按照特定算法进行加密后的数据
本接口的签名算法为
sign=Md5(phoneNum+ optCode+ timestamp)
签名过程中涉及到的C语言函数
字符串拼接
char str[50];
strcat(str,"testfan");
保存当前时间戳:web_save_timestamp_param("tStamp", LAST );
注意 引入 md5.h 文件进行算法解密
Action()
{
//定义 字符串类型
char cat[]; web_reg_find("Search=Body",
"SaveCount=find_cnt",
"Text=code\":\"0\"",
LAST); web_save_timestamp_param("tStamp", LAST ); //拼接 在一起
strcat(cat, lr_eval_string("135{phoneNum}"));
strcat(cat, "testfan");
strcat(cat, lr_eval_string("{tStamp}")); //CMd5 是调用的 MD5.h 这个文件中的 函数进行md5 解码的
lr_save_string(CMd5(cat), "singMd5"); lr_output_message("==============================%s",CMd5(cat)); web_add_header("Content-type", "application/json"); lr_start_transaction("singmd5"); web_custom_request("post", "Method=POST", "URL=http://localhost:8080/pinter/com/userInfo", "Body={\"phoneNum\":\"135{phoneNum}\",\"optCode\":\"testfan\",\"timestamp\":\"{tStamp}\",\"sign\":\"{singMd5}\"}", "TargetFrame=", LAST ); if(atoi(lr_eval_string("{find_cnt}")) > ){
lr_end_transaction("singmd5", LR_PASS); }else{ lr_end_transaction("singmd5", LR_FAIL); } return ;
}
运行日志如下:
Virtual User Script started at : -- ::
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0. for WINXP; build (Aug ::) [MsgId: MMSG-]
Run Mode: HTML [MsgId: MMSG-]
Run-Time Settings file: "C:\test_huzhenyu\Script\singn\\default.cfg" [MsgId: MMSG-]
Ending action vuser_init.
Running Vuser...
Starting iteration .
Starting action Action.
Action.c(): Registering web_reg_find was successful [MsgId: MMSG-]
Action.c(): Notify: Saving Parameter "tStamp = 1552736236160".
Action.c(): web_save_timestamp_param("web_save_timestamp_param") was successful [MsgId: MMSG-]
Action.c(): Notify: Parameter Substitution: parameter "phoneNum" = ""
Action.c(): Notify: Parameter Substitution: parameter "tStamp" = ""
Action.c(): Notify: Saving Parameter "singMd5 = b7f1b1f74de61497d65fd0027b8166fc".
Action.c(): ==============================b7f1b1f74de61497d65fd0027b8166fc
Action.c(): Warning -: The header being added may cause unpredictable results when applied to all ensuing URLs. It is added anyway [MsgId: MWAR-]
Action.c(): web_add_header("Content-type") highest severity level was "warning" [MsgId: MMSG-]
Action.c(): Notify: Transaction "singmd5" started.
Action.c(): Notify: Parameter Substitution: parameter "phoneNum" = ""
Action.c(): Notify: Parameter Substitution: parameter "tStamp" = ""
Action.c(): Notify: Parameter Substitution: parameter "singMd5" = "b7f1b1f74de61497d65fd0027b8166fc"
Action.c(): t=1018ms: -byte response headers for "http://localhost:8080/pinter/com/userInfo" (RelFrameId=, Internal ID=)
Action.c(): HTTP/1.1 \r\n
Action.c(): Content-Type: application/json;charset=UTF-\r\n
Action.c(): Transfer-Encoding: chunked\r\n
Action.c(): Date: Sat, Mar :: GMT\r\n
Action.c(): \r\n
Action.c(): t=1061ms: -byte chunked response overhead for "http://localhost:8080/pinter/com/userInfo" (RelFrameId=, Internal ID=)
Action.c(): 12a\r\n
Action.c(): t=1068ms: -byte chunked response overhead for "http://localhost:8080/pinter/com/userInfo" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): t=1077ms: -byte chunked response body for "http://localhost:8080/pinter/com/userInfo" (RelFrameId=, Internal ID=)
Action.c(): {"code":"","message":"success","data":{"id":,"userName":"鎬ц兘娴嬭瘯","password":nul
Action.c(): l,"age":,"gender":,"phoneNum":"","email":"beihe@testfan.com","address":"鍖椾
Action.c(): 含甯傛槍骞冲尯绉戞槦瑗胯矾106鍙烽櫌锛堝浗椋庣編鍞愯姳鍥患鍚堟ゼ锛\x893鍙锋ゼ1111","creat
Action.c(): eTime":null,"updateTime":null}}
Action.c(): HTML parsing not performed for Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://localhost:8080/pinter/com/userInfo" [MsgId: MMSG-]
Action.c(): t=1122ms: -byte chunked response overhead for "http://localhost:8080/pinter/com/userInfo" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): \r\n
Action.c(): Registered web_reg_find successful for "Text=code":""" (count=1) [MsgId: MMSG-26364]
Action.c(): Notify: Saving Parameter "find_cnt = 1".
Action.c(): web_custom_request("post") was successful, body bytes, header bytes, chunking overhead bytes [MsgId: MMSG-]
Action.c(): Notify: Parameter Substitution: parameter "find_cnt" = ""
Action.c(): Notify: Transaction "singmd5" ended with "Pass" status (Duration: 0.7485 Wasted Time: 0.4988).
Ending action Action.
Ending iteration .
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.