需要安装的软件
1、安装ImageMagick安装完成后,将其安装路径添加到环境变量path中
2、安装Tesseract-OCR
#define MAX_NAME_LEN 4 //定义验证码字符串的长度,这里是4位
int flen; //定义一个整型变量保存获得文件的大小
long filedes;
long filedess; //保存文件句柄
char file[256]="c:\\test1.jpg"; //保存文件路径及文件名
char result[4];//定义用于从文件中读取验证码的数组
web_set_max_html_param_len("2000000");
//设置页面接收最大的字节数,该设置应大于下载文件的大小
//下载含有验证码的网页元素,并将其保存到参数中
web_reg_save_param_ex(
"ParamName=pic",
"LB=",
"RB=",
SEARCH_FILTERS,
"Scope=Body",
"RequestUrl=*/action/ValidateCodeServlet*",
LAST);
//关联登录时与服务器产生的会话ID
web_reg_save_param_ex(
"ParamName=CorrelationParameter_1",
"LB=Set-Cookie: JSESSIONID=",
"RB=; Path",
SEARCH_FILTERS,
"Scope=All",
"RequestUrl=*/192.168.12.1:8080/*",
LAST);
//访问测试网站
web_url("192.168.12.1:8080",
"URL=http://192.168.12.1:8080/",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
EXTRARES,
"Url=/images/login_screenbg.jpg", ENDITEM,
"Url=/images/login_midbg.jpg",ENDITEM,
"Url=/images/login_contentbg.jpg", ENDITEM,
"Url=/images/login_btn.png", ENDITEM,
"Url=/images/login_input1.png",ENDITEM,
"Url=/images/login_input2.png",ENDITEM,
"Url=/images/reset_btn.png",ENDITEM,
LAST);
flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
//获得下载图片的文件大小
if(flen > 0)
{
if((filedes = fopen(file,"wb")) == NULL)
{
lr_output_message("oh cloud your Open File Failed!");
return -1;
}
fwrite( lr_eval_string("{pic}"),flen,1, filedes );
fclose( filedes );
}
system( "c:\\test1.bat" ); //在CMD命令行中运行OCR命令
/*
在C盘根目录下创建批处理文件test1.bat:
#进入到含有验证码图片的目录
c:
cd\
#转换下载的JPG文件为TIF
convert.exe -compress none -depth 8 -alpha offtest1.jpg test1.tif
#调用tesseract转换图片中的文件并保存到test1.txt文件中
tesseract.exe test1.tif test1
#执行完成后,自动退出命令行
exit
*/
if((filedess = fopen("c:\\test1.txt","rt")) == NULL)
{
lr_output_message("oh,cloud your Open File Failed!");
return -1;
}
fread( result,4,1,filedess );//从文件中读取验证码
fclose( filedess );
lr_output_message("result: %s", result); //打印出读取后的数据
lr_save_var(result,MAX_NAME_LEN,0,"VC" );
//保存指定位数的数据到变量中
web_submit_data("UserAction",
"Action=http://192.168.12.1:8080/action/UserAction",
"Method=POST",
"RecContentType=text/html",
"Referer=http://192.168.12.1:8080/",
"Snapshot=t2.inf",
"Mode=HTML",
ITEMDATA,
"Name=method","Value=login", ENDITEM,
"Name=goPage","Value=/index.html", ENDITEM,
"Name=currentPage","Value=/", ENDITEM,
"Name=username","Value=admin", ENDITEM,
"Name=password","Value=123456", ENDITEM,
"Name=ValidateCode","Value={VC}", ENDITEM, //输入获取的验证码
"Name=loginSubmit", "Value=登录", ENDITEM,
EXTRARES,
...
LAST);