cookie和session

1.cookie :在网站中,http请求是无状态的,也就是说即使第一次和服务器连接后并且登陆成功后,


第二次请求服务器一番不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,

第一次登陆后服务器返回一些数据(cooke)给浏览器,然后浏览器保存在本地。


当该用户发送第二次请求的时候,就会自动的把上次请求存储的cookie数据自动的携带给服务器

服务器通过浏览器袖带的数据就能判断当前是用户是哪个了。


use Net::SMTP;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use Switch;
use File::Temp qw/tempfile/;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);
my $CurrTime = time2iso(time());
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $now          = time();
$ua->agent('Mozilla/5.0');
my $cookie_jar = HTTP::Cookies->new(

    file           => 'lwp_cookies.txt',
    autosave       => 1,
    ignore_discard => 1
);
$ua->cookie_jar($cookie_jar);

if ( $cookie_jar){
$cookie_jar->load( $cookie_jar );
my $now          = time();
my $url="https://www.awfae.com/web/auth?method=%2Fwze%2Ftransfer%2Fcash%2Fin&amount=0.1&tradePassword=79e8fe923f031f46221a9283d0c9263a&_=$now";
my $response = $ua->get("$url");
if ($response->is_success) {
#print $response->content; 
  $r = $response->content;   

$str=encode("gbk",decode("utf8","$r"));
print "$str is $str
";
}
if ($str =~/用户未登录/){

##https://www.awfae.com/validcode/generate-validcode.do?0.36789621218278434
my $response = $ua->get("https://www.awfae.com/validcode/generate-validcode.do");
if ($response->is_success) {
   $r = $response->decoded_content;   
   # print $response->decoded_content;  # or whatever
}
else {
 die $response->status_line;
};
my ( $fh, $filename ) =
  tempfile( "wj_qrcode_XXXX", SUFFIX => ".jpg", DIR => 'c:\' );
binmode $fh;
print $fh $r;
close $fh;
print "登录二维码已经下载到本地 [ $filename ] 
";

##打开图片
system("start $filename ");
my $validCode = <STDIN>;
chomp $validCode ;
 my $login_url = 'https://www.awfae.com/business/dispatch_post.do';
                
 my $res = $ua->post($login_url,{
	                       'action'=>'doLogin',
                          'userName'=>'18072722237',
						  'followId'=>'',
                          'userPass'=>'79e8fe923f031f46221a9283d0c9263a',
                          'validCode'=> "$validCode"
                                                });
			 print "---------------
";

my $head =$res->header('Location');	
;				
print $res->header('Location');	
print "
";	

my $response = $ua->get("$head");
if ($response->is_success) {
  #print $response->content; 
  $r = $response->content;   

$str=encode("gbk",decode("utf8","$r"));
print "$str == $str
";
}
my $now          = time();
my $url="https://www.awfae.com/web/auth?method=%2Fwze%2Ftransfer%2Fcash%2Fin&amount=0.1&tradePassword=79e8fe923f031f46221a9283d0c9263a&_=$now";
my $response = $ua->get("$url");
if ($response->is_success) {
#print $response->content; 
$r = $response->content;   

$str=encode("gbk",decode("utf8","$r"));
print "$str is $str
";
}
}
}
##http://www.awfae.com/login.html?info=u767bu5f55u5931u8d25u003au7528u6237
##http://www.awfae.com/login.html?info=登录失败:用户
##http://www.awfae.com/account/myAccount.html?subPage=/account/dashBoard.html&uuid=20150925104158452da9e0c7979d4438&

# 响应头:
# 原始头信息
# Connection	
# close
# Content-Language	
# zh-CN
# Content-Length	
# 0
# Date	
# Fri, 01 Sep 2017 00:57:34 GMT
# Location	
# https://www.awfae.com/account/myAccount.html?subPage=/account/dashBoard.html&uuid=20150925104158452da9e0c7979d4438
# &
# Server	
# Apache-Coyote/1.1
# Set-Cookie	
# rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Thu, 31-Aug-2017 00:57:34 GMT
# X-Frame-Options	
# SAMEORIGIN



cookie内容:

#LWP-Cookies-1.0
Set-Cookie3: ZJZCJSESSIONID=5e44f46d-d25b-4c6d-8dd3-89cb3b0fae4b; path="/"; domain=www.awfae.com; path_spec; secure; discard; HttpOnly; version=0

cookie是存储数据的一种格式,第2次请求时浏览器自动加载cookie,发送给服务端



cookie 是保存在浏览器里,cookie里存储sessionid,下次发送时会加载cookie


session 是保存在服务器中

session可以设置过期时间

原文地址:https://www.cnblogs.com/hzcya1995/p/13349537.html