免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3275 | 回复: 7
打印 上一主题 下一主题

Mojo::UserAgent设置了cookie没有生效 [复制链接]

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-02-22 14:45 |只看该作者 |倒序浏览
本帖最后由 只是一个红薯 于 2016-02-22 17:37 编辑

现在问题是这样的:
1.登录一个网站,返回两条cookie信息。
2.新添加cookie2,使用这3条cookie信息请求另一个页面。
设置了cookie2,但没有生效

请问下怎么改才能添加新的request cooike
  1. use 5.018;
  2. use warnings;
  3. use JSON;
  4. use Mojo::UserAgent;
  5. use Data::Dumper;
  6. use Mojo::UserAgent::CookieJar;
  7. use URI::Escape;
  8. my $loginData={
  9. email                => "test@163.com",
  10. password        => "achinaunix=",
  11. };
  12. my $loginData_json = encode_json $loginData;
  13. my $ua=Mojo::UserAgent->new;
  14. my $jar = Mojo::UserAgent::CookieJar->new;
  15. $ua     = $ua->max_redirects(1);
  16. my $url='https://xxxx/login';
  17. my $response = $ua->post($url=> json => $loginData,'content-type' =>'application/json');
  18. say $response->res->body;
  19. #say $_ for @{$response->res->cookies}; #resoonse里有两条cookie信息

  20. $jar->add(
  21.   Mojo::Cookie::Response->new(
  22.     name   => 'name',
  23.     value  =>  'value',
  24.     domain => '.talkingdata.com',
  25.     path   => '/'
  26.   )
  27. );
  28. $ua = $ua->cookie_jar($jar); #[color=Magenta]没有生效[/color]

  29. open F,'>','b';
  30. say F Dumper($ua->get('https://xxxx.com'=>{'Referer'=>'https://xxxx.com'}));
复制代码
@iakuf
[@]py[/@]如果方便,帮忙看看,谢谢

论坛徽章:
0
2 [报告]
发表于 2016-02-25 20:57 |只看该作者
本帖最后由 RZL_01 于 2016-02-26 20:03 编辑
  1. print  Dumper($jar);
复制代码
是不是数据有加上去了吗?

论坛徽章:
0
3 [报告]
发表于 2016-02-26 20:19 |只看该作者

  1. my $un='';
  2. my $pw='';
  3. my $ref='hm';
  4. my $loginUrl='';
  5. my $postUrl='1‘;

  6. my $loginform={
  7.         'password' => $pw,
  8.         'referer' => $ref,
  9.         'username' => $un,
  10.         };
  11. my $ua=Mojo::UserAgent->new;
  12. $ua     = $ua->max_redirects(1);
  13. $ua->get($loginUrl);
  14. $ua->post($postUrl,{DNT => 1} => form => $loginform);
  15. my $jar=$ua->cookie_jar;
  16. my $uaa=Mojo::UserAgent->new;
  17. $uaa->cookie_jar($jar);
  18. my $tx=$uaa->get($ref);
复制代码

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
4 [报告]
发表于 2016-02-26 20:57 |只看该作者
好像会覆盖之前的cookie回复 3# RZL_01


   

论坛徽章:
0
5 [报告]
发表于 2016-02-27 00:14 |只看该作者
我个人认为,你的代码jar是新建的,没有保存或加载原有的cookies记录只添加了一条新纪录,你dumper出来只有一条新加记录,那是正常的吧。

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
6 [报告]
发表于 2016-02-29 12:03 |只看该作者
回复 5# RZL_01


    怎么加在以前的?
现在我请求都这样写,不过没在jar里面,比较费劲
  1. my $header =  $ua->get('https://www.xxxx.com'
  2.          =>{
  3.                 Referer=>'https://www.xxxx.com',
  4.                 Cookie=> $new_cookie,
  5.         })->res->headers->to_hash;
复制代码

论坛徽章:
0
7 [报告]
发表于 2016-03-01 12:31 |只看该作者
本帖最后由 RZL_01 于 2016-03-01 12:33 编辑
  1. use Mojo::UserAgent;
  2. use Data::Dumper;
  3. use Mojo::UserAgent::CookieJar;

  4. my $url='xxxxxxx';
  5. my $ua=Mojo::UserAgent->new;
  6. $ua     = $ua->max_redirects(1);
  7. $ua->get($url);
  8. my $jar=$ua->cookie_jar;
  9. $jar->add(
  10.   Mojo::Cookie::Response->new(
  11.     name   => 'name',
  12.     value  =>  'value',
  13.     domain => 'chinaunix.net',
  14.         max_age => '0',
  15.     path   => '/'
  16.   )
  17. );
  18. print  Dumper($jar);

  19. my $newurl='aaaaaa';
  20. my $uaa=Mojo::UserAgent->new;
  21. $uaa->cookie_jar($jar);
  22. my $tx=$uaa->get($newurl);
  23. print  Dumper($uaa->cookie_jar);
复制代码

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
8 [报告]
发表于 2016-03-03 20:03 |只看该作者
多谢,有空试下看行不,最近工作太忙回复 7# RZL_01


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP