简体   繁体   English

在WWW :: Mechanize中直接使用`get`访问URL时,如何保持身份验证?

[英]How do I remain authenticated when accessing an URL directly with `get` in WWW::Mechanize?

I'm new to WWW::Mechanize but I know how to login and then how to use the follow_link method to access other pages while I'm logged in. But when I want to access a link directly that is not on the page, I lose my authentication. 我是WWW :: Mechanize的新手,但是我知道如何登录,然后在登录后如何使用follow_link方法访问其他页面。但是当我想直接访问该页面上没有的链接时,我丢失了身份验证。

Quick sample of the problem: 问题的快速示例:

my $LoginURL = "http://www.website.com/user/login.jsp?";
my $DirectURL= "/Somefile?param1&param2";
$mech = WWW::Mechanize->new();
$mech->get($LoginURL);
$mech->submit_form( ... ); # fields and stuff, works fine.
$mech->get($DirectURL);    # This part fails and I'm using the direct URL.

I understand how to login and I can navigate around with the follow_link method, but when I want to access a URL that is not on the page using the get method I simply lose my authentication. 我了解如何登录,并且可以使用follow_link方法导航,但是当我想使用get方法访问页面上没有的URL时,我只会丢失身份验证。

What will I have to do in order to remain authenticated when I want to access a direct URL not located on the current page? 当我想访问不在当前页面上的直接URL时,为了保持身份验证我该怎么办?

Since HTTP is a stateless protocol, each request is independent of the previous one. 由于HTTP是无状态协议,因此每个请求都独立于前一个请求。 In versions previous to HTTP 1.1, the default behavior for HTTP transactions is for a client to contact a server, send a request, and receive a response, and then both the client and server disconnect the TCP connection. 在HTTP 1.1之前的版本中,HTTP事务的默认行为是客户端与服务器联系,发送请求和接收响应,然后客户端和服务器都断开TCP连接。 If the client needs another resource on the server, it has to reestablish another TCP connection, request the resource, and disconnect. 如果客户端需要服务器上的其他资源,则它必须重新建立另一个TCP连接,请求资源并断开连接。 If you have successfully authenticated, the target website might "forget" that when you trying to access the next page. 如果您已成功通过身份验证,则当您尝试访问下一页时,目标网站可能会“忘记”该身份。 Try to see which value is included in the HTTP "Connection" header. 尝试查看HTTP“连接”标头中包含的值。 It should be "keep-alive" and not "TE, close". 它应该是“保持活动”,而不是“ TE,关闭”。 If this is the issue try to modify the "Connection" header. 如果这是问题,请尝试修改“连接”标题。 You can try something like this: 您可以尝试如下操作:

  $mech->add_header(
  "Connection" => "keep-alive",
  "Keep-Alive" => "115");

Hope it helps. 希望能帮助到你。

$mech->get() is an overload of LWP::UserAgent. $ mech-> get()是LWP :: UserAgent的重载。 It expects absolute URLs. 它需要绝对URL。

my $uri = $mech->uri();           # Get the current URI
my $uri->path_query($DirectURL);  # Replace the path with file path
$mech->get($uri->as_string);      # Returns http://www.website.com/Somefile?param1&param2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM