LR模拟下载文件脚本

上一篇 / 下一篇  2016-02-24 15:24:44 / 天气: 晴朗 / 心情: 平静 / 个人分类:LoadRunner


花了点时间,写了下LR模拟下载的脚本,记录下,希望对某些同学有些帮助啊。
其实一有东西都是写在了自己的为知笔记上,这个账号也有多年了,没事了也来写写吧。

Action()
{
    //下载投票文件脚本,下载需要登录的
    int lflen;//文件大小
    long lfbody;//响应数据内容大小
    char filePath[50];//存放文件路径
    
    //使用cookie方式登录用户,不清楚过一段时间该cooike是否失效,有可能会失效,到时候从新写一下cookie信息就可以了
    web_add_cookie("_ga=GA1.2.1023610577.1452156173; DOMAIN=w5.31huiyi.com");
    web_add_cookie("Hm_lvt_7f1d2a1bbfe70930077693fb73d7f592=1453095607,1454381761,1454384469,1454384553; DOMAIN=w5.31huiyi.com");
    web_add_cookie("_jzqa=1.333680718502497850.1452156173.1454384470.1454384553.9; DOMAIN=w5.31huiyi.com");
    web_add_cookie("LoginCookieName_31huiyi=OLk4qNmkGwah1FjTKICua7115MXqY6ZUGJ5/u5cH0lMOpxduMuDMLqaWre4RJZJkf3ASrjmdI9iDT/JwS0UhViYEbV6i79UYLaSBch8sd6iPBtrTCu/1rG6J0vPmeYGNQox8M/w8Wo7MpriIvXXZXQOo7tQIQ826KWH75B6GPVC6TRqXDTMAUjliEaO6pw5gc4E1hKDUFmI1MsGXhZc4pJsy3MSHZJ2rfRg+TsjhNYIZ/KP/xvPEwIOgrsdmlhmKm36OQ7zIgpE=; DOMAIN=w5.31huiyi.com");
    web_add_cookie("WallBackCookie=OFI4U0VZLytFZ0htV2wrSnovNW4vc29LZFJUK2UxV1hzSXI1UnIwUzd3aTBPMHlnMXBTNlJEY3Y4MXUrTng3QWVWazVTWHNjL3hLc1N2N3BsU2lLQVFaUE51d1NYZXNRdnZsWXBobVE0RityQXZSd0ErNzRONGxvZ3lJN1k3QXg2alBqcVplRjZDZ3Y4SklxcEhxWTBralkvZXJ3VGcwU09OeS9POGtNZEErWTNEaTF6RXBPUkhEZkg5c1B0SG53NVdWMVNSU0ZGVjhDZWNBMk9CcWZOWENUUVR5VCt5T0tYVGJqTDN1SnlpUThSRGNadUgxclZBWWpzL1lXTDh2ZQ==; DOMAIN=w5.31huiyi.com");

    //获得服务器自动生成的文件名字
    web_reg_save_param("getFileName",
        "LB=attachment; filename=",
        "RB=\r\n",
        "Ord=1",
        "NotFound=ERROR",
        "Search=All",
        LAST);
    
    web_url("56c6d5d763419849f89aea0d"
        "URL=http://w5.31huiyi.com/vote/vote/56c6d5d763419849f89aea0d"
        "TargetFrame="
        "Resource=0"
        "RecContentType=text/html"
        "Referer="
        "Snapshot=t126.inf"
        "Mode=HTML"
        EXTRARES
        "Url=http://s4.31huiyi.com/bwallv4/dist/fonts/glyphicons-halflings-regular.eot?"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-settings.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-lottery.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-vote-active.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-crazy.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-content.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-guess.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-reward.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-roaster.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-ibeacon.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-vote-item.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/img/w-icon-picture.png"ENDITEM
        "Url=http://s4.31huiyi.com/bwallv4/dist/css/blue.png"ENDITEM
        "Url=/Home/ExporWall?searchKey=1261356|56c6d5d763419849f89aea0d|1"ENDITEM
        LAST);
    
    /* 开始下载文件 */
    web_custom_request("ExportVote"
        "URL=http://w5.31huiyi.com/Vote/ExportVote?sendtype=1&WallId=56c6d5d763419849f89aea0d"
        "Method=POST"
        "TargetFrame="
        "Resource=0"
        "RecContentType=text/javascript"
        "Referer=http://w5.31huiyi.com/vote/vote/56c6d5d763419849f89aea0d"
        "Snapshot=t128.inf"
        "Mode=HTML"
        "EncType="
        LAST);
    
    web_submit_data("DowloadContent"
        "Action=http://w5.31huiyi.com/Home/DowloadContent"
        "Method=POST"
        "TargetFrame="
        "RecContentType=application/json"
        "Referer=http://w5.31huiyi.com/vote/vote/56c6d5d763419849f89aea0d"
        "Snapshot=t129.inf"
        "Mode=HTML"
        ITEMDATA
        "Name=searchKey""Value=1261356|56c6d5d763419849f89aea0d|1"ENDITEM
        LAST);
    
    //设置参数的最大长度,注意该值必须大于文件的大小
    web_set_max_html_param_len("20000");
    //获取响应的全部内容,作为文件内容保存
    web_reg_save_param("fcontent",
        "LB=",
        "RB=",
        "Search=Body",
        LAST);
    //下载的文件的具体链接,录制脚本的时候这个请求是没有抓取到的,需要手工自己写进来
    web_url("web_url",
        "URL=http://w5.31huiyi.com/Home/ExporWall?searchKey=1261356|56c6d5d763419849f89aea0d|1",
        "TargetFrame=",
        "Resource=0",
        "Referer=",
        "Mode=HTML",
        LAST);

    
    /* 结束下载 */
    strcpy(filePath,"C:\\Users\\31huiyi\\Downloads\\");
    strcat(filePath,lr_eval_string("{getFileName}"));
    lr_message("路径是:%s",filePath);
     //获取响应中的文件长度
    lflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
    lr_output_message("页面字节数是:%d",lflen);
    if(lflen>0){
         //以写方式打开文件
        if((lfbody=fopen(filePath,"wb"))==NULL){
            lr_output_message("Open File Failed!");
            return -1;
        }
        //写入文件内容
        fwrite(lr_eval_string("{fcontent}"),lflen,1,lfbody);
        //关闭文件
        fclose(lfbody);
    }

    return 0;
}

TAG: 下载文件

 

评分:0

我来说两句

Open Toolbar