如何使用perl模块LWP::UserAgent指定存储在文件中的cookie

ozxc1zmp  于 2022-12-19  发布在  Perl
关注(0)|答案(1)|浏览(169)

我有下面的代码片段,它使用从另一个perl程序收集的cookie,通过perl模块LWP::UserAgent登录到sharepoint网站。

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::CookieJar::LWP ();
use HTTP::Cookies;

my $resultXML='C:\Docs\Result.txt';
my $url="https://[mydomain].sharepoint.com/_api/contextinfo";
        
my $cookie_jar_obj = HTTP::Cookies->new();
$cookie_jar_obj->load( $resultXML );
my $lwp = LWP::UserAgent->new();
$lwp->cookie_jar( $cookie_jar_obj ); 
        
print $lwp->cookie_jar->as_string() . "\n";

my $response = $lwp->request(POST $url);
print $response->content . "\n";

其中存储在变量$resultXML中的文件包含以下Cookie信息:

#LWP-Cookies-1.0
Set-Cookie3:rtFa=UROIhi84Sxr8o0DZHximQjlCOUI2QzItMzRDMC00N0Q1LTgyQ0EtMzI1MzhCRjNERTZDjQiIQbx0/QTRCNtpvjQTaZEcjpYFSkKHsPK+d1Nlnx6MuaDI/Xs21W7orysOeYPRRoDCCX/3ZtL61T3SHYfMkPeqXTJ4rloliGHsN7tuQXeI6OYAXXs8ysayP4VzpJyQEm+S0zvjkVLLtanQ5RDfQ1bnYq9ayu7L5HDJDNaKQhmhXmTdodwTlvwJRRyWgLL/GCuWWyAXEppy9Ta7PzpO76sb9c91ssScjDJY/Fw7TIPipleMgjjnHud1Os/NAihtTxTMWxS5XpiZbCh6DtvPwYyZXE54nsyhIekRvrixFQ0PeZinWpAkUAAAA=; domain=sharepoint.com; path=/; SameSite=None; secure; HttpOnly;FedAuth=77u/PD94bWwgdmVyc2lvbj0wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+VjksMGguZnxtZW1iZXJzaGlwfDEwMDNiZmZkOWUwNzg1MmVAbGl2ZS5jb20sfGFnb3R0c0B0bG1seW9uLm9ubWljcm9zb2Z0LmNvbSwxMzI2MDYzNzYxMjAwMDAwMDAsMTMyMzk3ODg0ODcwMDAwMDAwLDEzMjYwNzI0MDEzMzA3NjA2MSwwLjAuMC4wLDIsYjliOWI2YzItMzRjMC00N2Q1LTgyY2EtMzI1MzhiZjNkZTZjLCwsMGU4ZmUtYTliZS00OGI2LWEzYTgtYmM5OGViZjQ0ZDM1LDIyZDhlOGZlLWE5YmUtNDhiNi1hM2E4LWJjOThlYmY0NGQzNSwsMCwwLDAsLCwsMjY1MDQ2NsLCwscjEyUzEwdzJWT2ZKNXVFRWVLWFBwUUxRYnIrdnp6QnM2R2hnMGQxOTlzcGZ1SnBpSWx0S2pvZmJSVnVVY2FzUzd0TkxVOUxlcklZenA1cndPYmhMeDJ0V2pnUmZidElnSVZRQnZWSGxZWlVocTBpN3lyWkVkMHNnRjFvVTAwY1B3Qmt2WUdrRDZkK2Z3NzRjFCWFFvSjBDb0d4UXVYd01DT2tQb0RhWEdDQkdVbmt2Si9Bc3BCbm5Jb1JXSUxBRjgrZ2JLTjU3RURGbFU0V09XZGdiZFdxM3JLVWg5aGs5NnhRRXVad25laXphRjR3STlkejJvTEROYnA0NDNZWm8rZXN6YmlLR2tqdkhBRGx2L1B5SjllRlBDNHhtNS9qVkQ2NUtSUk9YYnN4MEhxNnJSVVdzVHhPdzU5VXJ1M2lBPT08L1NQPg==; path=/; SameSite=None; secure; HttpOnly;

如果我从"rtFA ="部分开始将此信息复制并粘贴到"Advanced REST client" chrome扩展中,我会得到成功的响应。
但是在打印条目时

$lwp->cookie_jar->as_string()

在结果中有额外的双引号字符。

Set-Cookie3:rtFa="UROIhi84Sxr8o0DZHximQjlCOUI2QzItMzRDMC00N0Q1LTgyQ0EtMzI1MzhCRjNERTZDjQiIQbx0/QTRCNtpvjQTaZEcjpYFSkKHsPK+d1Nlnx6MuaDI/Xs21W7orysOeYPRRoDCCX/3ZtL61T3SHYfMkPeqXTJ4rloliGHsN7tuQXeI6OYAXXs8ysayP4VzpJyQEm+S0zvjkVLLtanQ5RDfQ1bnYq9ayu7L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; path=/; SameSite=None; secure; HttpOnly;

如果我复制并粘贴此信息,同样,从"rtFA ="部分开始到"Advanced REST client" chrome扩展中,我会得到错误403 Forbidden error,这正是我在
打印$lwp-〉cookie_jar-〉as_string()."\n";
声明。
我怎样才能阻止perl插入这些额外的双引号(")标记,这些标记似乎阻止了我的perl脚本连接到网站?

7dl7o3gd

7dl7o3gd1#

不要使用as_string的结果来获取cookie值。这通常不适用于连接。查看HTTP::Cookies中的add_cookie_header子例程,注意到它们没有使用as_string来计算任何东西。尽管它引用了除了值之外的所有内容。
我认为首先要考虑的是给HTTP::Cookies取了什么,也许变量名中的XML只是一个不好的名字,但是数据不是XML,如果你把 Result.txt 中的数据弄乱了,那么你的问题很早就出现了:

my $resultXML='C:\Docs\Result.txt';
my $url="https://[mydomain].sharepoint.com/_api/contextinfo";
        
my $cookie_jar_obj = HTTP::Cookies->new();
$cookie_jar_obj->load( $resultXML );

相关问题