概述:
本文主要是学习web_reg_save_param、lr_eval_string、sprintf、strstr、strcpy、strncpy、strlen、strcat等函数,同时处理重复数据、敏感字的替换。
疑问:
如果两个网站默认的编码不一样,一个是GB2312,一个是UTF-8,如何解决呢?难道一定要进行编码转换吗?Loadrunner是否有现成的函数可以实现?
脚本:
#include "web_api.h"
Action()
{
int i,j,g,f,e,offset;
int NewsCount,ContentCount,h;
char Str_Tem[100];
char Str_NewsCount[100][100];
char Str_NewsId[100],Str_NewsIdd[100];
char Str_NewsTitle[1000];
char Str_NewsContent[2000];
char Str_News[2000],Str_temp[2000];
char *position;
char search_str[] = "<";
char search_title[] = "_";
//登录要发帖的网站
web_url("Login.aspx",
"URL=http://WebPassport.com.com/Login.aspx",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t2.inf",
"Mode=HTML",
EXTRARES,
LAST);
web_submit_form("Login.aspx_2",
"Snapshot=t3.inf",
ITEMDATA,
"Name=_username", "Value=烟三", ENDITEM,
"Name=_password", "Value=541111", ENDITEM,
"Name=CheckboxSaveInfo", "Value=<OFF>", ENDITEM,
"Name=Login", "Value=登录", ENDITEM,
EXTRARES,
LAST);
//从其他网站获取新闻
//1、获取新闻ID
web_reg_save_param("NewsId","NOTFOUND=warning","LB=http://ent.ifeng.com/idolnews/hk/200803/","RB=.shtml","ORD=ALL",LAST);
web_url("ent.ifeng.com",
"URL=http://ent.ifeng.com/",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTTP",
EXTRARES,
LAST);
NewsCount=atoi(lr_eval_string("{NewsId_count}"));
//2、剔出其中重复的ID
f=0;
for (i=1;i<=NewsCount;i++)
{ g=0;
sprintf(Str_Tem,"{NewsId_%d}",i);
sprintf(Str_NewsId,"%s",lr_eval_string(Str_Tem));
for (j=i+1;j<=NewsCount;j++)
{ sprintf(Str_Tem,"{NewsId_%d}",j);
sprintf(Str_NewsIdd,"%s",lr_eval_string(Str_Tem));
if (strcmp(Str_NewsId,Str_NewsIdd)==0) g++;
}
if (g==0)
{ //lr_message("The NewsID is: %s@%d",Str_NewsId,f);
strcpy(Str_NewsCount[f],Str_NewsId);
f++;
}
}
//lr_message("The distinct NewSID Number is: %d",f);
//3、从每条新闻中获取新闻标题与内容
for (h=0;h<f;h++)
{
lr_save_string(Str_NewsCount[h],"News_Id");
//lr_message("%s",lr_eval_string("{News_Id}"));
web_set_max_html_param_len("10240");
web_reg_save_param("NewsTitle","NOTFOUND=warning","LB=<title>","RB=</title>",LAST);
web_reg_save_param("NewsContent","NOTFOUND=warning","LB=<P><FONT face=Verdana>","RB=</FONT></P>","ORD=ALL",LAST);
web_url("New Page",
"URL=http://ent.ifeng.com/idolnews/hk/200803/{News_Id}.shtml",
"Resource=0",
"RecContentType=text/html",
"Referer=http://ent.ifeng.com/",
"Snapshot=t3.inf",
"Mode=HTTP",
EXTRARES,
LAST);
ContentCount=atoi(lr_eval_string("{NewsContent_count}"));
//lr_message("ContentCount is:%d",ContentCount);
//4、控制新闻内容的长度以及剔除废信息
sprintf(Str_NewsContent,"");
for (e=1;e<=ContentCount;e++)
{ sprintf(Str_Tem,"{NewsContent_%d}",e);
sprintf(Str_temp,lr_eval_string(Str_Tem));
//lr_output_message("%s",Str_temp);
position = (char *)strstr(Str_temp, search_str);
if (position!=NULL)
{ ōffset = (int)(position - Str_temp );
//lr_output_message("The string \"%s\" was found at position %d", lr_eval_string(Str_Tem), offset);
strncpy(Str_News,lr_eval_string(Str_Tem),offset);
continue;
}
else strcpy(Str_News,lr_eval_string(Str_Tem));
if (strlen(Str_NewsContent)+strlen(Str_News)<2000)
strcat(Str_NewsContent,Str_News);
}
//lr_output_message("The44 is:%d",strlen(Str_NewsContent));
//lr_message("The h is:%d",&h);
//lr_message("The h is:%d",Str_NewsContent);
//5、剔出标题中废数据
sprintf(Str_temp,lr_eval_string("{NewsTitle}"));
position = (char *)strstr(Str_temp, search_title);
lr_output_message("The is:%s",Str_temp);
if (position!=NULL)
{ ōffset = (int)(position - Str_temp);
lr_output_message("The2 is:%d",offset);
strncpy(Str_NewsTitle,Str_temp,offset);
}
else strcpy(Str_NewsTitle,Str_temp);
//6、内容为视频,无法获取文字内容处理
if (strlen(Str_NewsContent)==0) sprintf(Str_NewsContent,"视频暂缺!");
lr_save_string(Str_NewsContent,"NewContent");
lr_save_string(Str_NewsTitle,"NewsTitlee");
//lr_message("The Title is:%s",lr_eval_string("{NewsTitlee}"));
//lr_message("The Content is:%s",lr_eval_string("{NewContent}"));
//到指定的网站发帖
web_url("bar_161.html",
"URL=http://WebPost.com.com/bar_161.html",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t4.inf",
"Mode=HTML",
LAST);
web_submit_data("add_topic.do",
"Action=http://WebPost.com.com/add_topic.do",
"Method=POST",
"RecContentType=text/html",
"Referer=http://WebPost.com.com/bar_161.html",
"Snapshot=t9.inf",
"Mode=HTML",
ITEMDATA,
"Name=posturl", "Value=http://WebPost.com.com/bar_161.html", ENDITEM,
"Name=Username", "Value=烟三",ENDITEM,
"Name=postTitle","Value={NewsTitlee}", ENDITEM,
"Name=postContent", "Value={NewContent}", ENDITEM,
"Name=", "Value=", ENDITEM,
"Name=validation_code", "Value=#valiCode1#",ENDITEM,
"Name=validationcode_input", "Value=", ENDITEM,
"Name=BarID","Value=161", ENDITEM,
"Name=TopicID", "Value=", ENDITEM, LAST);
lr_think_time(10);
}
return 0;
}