- 论坛徽章:
- 0
|
use strict;
use warnings;
use LWP;
use LWP::ConnCache;
# main procedure #
my $ua = LWP::UserAgent->new( );
# setup attribution #
push @{ $ua->requests_redirectable }, 'POST'; # post之后能自动打开redirect页面
$ua->agent('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; AskTbFXTV5/5.11.3.15590)');
$ua->cookie_jar( {} ); #打开cookie功能
$ua->timeout(15);
$ua->conn_cache( LWP::ConnCache->new() ); # keep alive
if( &login ){
print "登录成功!\n";
}else{
print "登录失败!\n";
}
sub login {
my $acc = shift || '这里填默认使用的账号';
my $pw = shift || '这里填默认使用的密码';
my $verifycode;
my $response = $ua->get('http://www.xckjpx.net/user/login_inc/checkcode/checkcode.asp');
if ($response->is_success) {
# 抓取验证码图片 #
open my $check_code_image, "> verifycode.BMP" #默认验证图片的位置在当前目录
or die "$!";
binmode($check_code_image);
print $check_code_image $response->content;
close $check_code_image; #关闭print对句柄的控制
{ # 输入验证码并登录 #
my $pid = fork();
if ($pid) {
# parent
print "> enter verifycode:";
chomp( $verifycode = <> );
$verifycode=~ s/\s//g; #删除空白
} elsif ($pid == 0) {
# child
sleep 10;
`mspaint verifycode.BMP`;
exit 0;
} else {
die "couldnt fork: $!\n";
}
waitpid($pid,0);
my $response = $ua->post(
'http://www.xckjpx.net/user/check.asp',
[username => $acc, password => $pw, xhccl => 'login', verifycode => $verifycode, Sub_sub =>''],
);
if ($response->is_success) {
return $response->content =~ /location='login.asp'/ ? 0 : 1; # 根据是否重定向页面到登录界面来判断登录是否成功
# 登录成功返回 1 登录失败返回 0
}
else {
print '> failed to post data for login: ' . $response->status_line . "\n";
}
} #
}
else {
print '> failed to get verifycode: ' . $response->status_line . "\n";
}
}
加个自动打开图片的功能,免得还要自己找图片 |
|