获取cookie值
```
sub getCookie{
my($ip,$port)=@_;
my $ua = LWP::UserAgent->new(
protocols_allowed => [ 'http', 'https' ],
timeout => 30,
ssl_opts => { verify_hostname => 0},
);
$username='admin';
$password='admin';
my $arg={
'username' => $username,
'password' => $password,
};
$url="https://$ip:$port/vapi/extjs/access/ticket";
my $resp = $ua->post($url,$arg,'Content_Type' => 'form-data');
my $status=$resp->status_line;
my $header=$resp->headers_as_string;
my $content = $resp->content;
if ($status=~ /200/)
{
@array=split(/\n/,$header);
foreach $rock (@array)
{
if ($rock=~/^Set-Cookie/)
{
@Setcookie=split(/\s+/,$rock);
$Setcookie[1]=~s/;$
my $cookie=$Setcookie[1];
$content=$cookie;
last;
}
}
}
return $content;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
发送get请求
调用上面的getcookie函数,获取cookie值
sub https_Get {
my ( $url,$cookie) = @_;
my $ua = LWP::UserAgent->new(
protocols_allowed => [ 'http', 'https' ],
timeout => 30,
ssl_opts => { verify_hostname => 0 },
);
$ua->default_header('cookie' => "$cookie" );
my $response = $ua->get($url);
return $response->{_content};
}
发送post请求
sub https_Post{
my($url,$arg,$cookie) = @_;
my $ua = LWP::UserAgent->new(
protocols_allowed => [ 'http', 'https' ],
timeout => 30,
ssl_opts => { verify_hostname => 0},
);
$ua->default_header('cookie' => "$cookie" );
$resp = $ua->post($url,$arg,'Content_Type' => 'form-data');
$content = $resp->content;
return $content;
}