使用LWP :: UserAgent时403错误,但不使用WWW :: Mechanize(403 error when using LWP::UserAgent but not with WWW::

系统教程 行业动态 更新时间:2024-06-14 16:53:13
使用LWP :: UserAgent时403错误,但不使用WWW :: Mechanize(403 error when using LWP::UserAgent but not with WWW::Mechanize)

我试图使用Perl5和LWP :: UserAgent访问一个站点。 但是在连接时,脚本会以“403 access denied”消息消失。 奇怪的是它使用WWW :: Mechanize完美无缺,但获取代码完全相同。 通常我会怀疑用户代理是原因,但正如前面提到的那样,代码在两种情况下都是相同的。

WWW :: Mechanize和LWP :: UserAgent处理可能导致此问题的请求有何不同?

以下是一些演示两种不同方法的示例代码。

# Mechanize use strict; use warnings "all"; use WWW::Mechanize; my $mech = WWW::Mechanize->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $mech->get("www.foo.com"); # LWP use strict; use warnings "all"; use LWP::UserAgent; my $ua = LWP::UserAgent->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $r = $ua->get("www.foo.com");

I am trying to access a site using Perl5 and LWP::UserAgent. However upon connecting, the script dies with a "403 access denied" message. The weird part is that it works flawlessly using WWW::Mechanize but the fetch code is the exact same. Normally I'd suspect the user agent being the cause but as mentioned before the code is the same in both cases.

Is there a difference in how WWW::Mechanize and LWP::UserAgent handle requests that could cause this issue?

Here is some sample code that demonstrates two different approaches.

# Mechanize use strict; use warnings "all"; use WWW::Mechanize; my $mech = WWW::Mechanize->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $mech->get("www.foo.com"); # LWP use strict; use warnings "all"; use LWP::UserAgent; my $ua = LWP::UserAgent->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $r = $ua->get("www.foo.com");

最满意答案

对于LWP :: UserAgent和WWW :: Mechanize,都没有记录agent_alias参数。 而且也没有agent_alias参数实现。 相反, 在两种情况下都会忽略参数 ,并使用内置默认值 。 但默认是不同的 。 对于WWW :: Mechanize,有一个agent_alias 方法 。 从文档 :

...例如,

$mech->agent_alias( 'Windows IE 6' );

将您的用户代理设置为

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

根据LWP :: UserAgent的文档,您实际想要使用的参数被正确地称为agent ,默认为libwww-perl/#.###是版本号)。 使用WWW :: Mechanize可以使用相同的参数,但记录的不同默认值是WWW-Mechanize/#.## 。

There is no agent_alias argument documented, neither for LWP::UserAgent nor for WWW::Mechanize. And there is also no agent_alias argument implement. Instead the argument gets ignored in both cases and it uses the builtin default. But the default is different. There is a agent_alias method though for WWW::Mechanize. From the documentation:

...For instance,

$mech->agent_alias( 'Windows IE 6' );

sets your User-Agent to

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

According to the documentation of LWP::UserAgent the argument you actually want to use is properly called agent and defaults to libwww-perl/#.### (the #.### being the version number). With WWW::Mechanize the same argument can be used but the documented different default is WWW-Mechanize/#.##.

更多推荐

本文发布于:2023-04-06 01:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/95d0fe07d29c0d10237743f48f44afdd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但不   错误   LWP   UserAgent   Mechanize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!