简体   繁体   English

如何获取WWW :: Mechanize以正确处理带有URI片段的302重定向?

[英]How do I get WWW::Mechanize to properly handle 302 redirects with URI fragments?

I have a webpage which redirects to another url in the form of http://www.example.com/url.html#midpage . 我有一个网页,该网页以http://www.example.com/url.html#midpage的形式重定向到另一个URL。

I'm wondering if there's anyway for WWW::Mechanize to follow http://www.example.com/url.html instead of http://www.example.com/url.html#midpage ? 我想知道是否有WWW :: Mechanize跟随http://www.example.com/url.html而不是http://www.example.com/url.html#midpage

WWW::Mechanize subclasses LWP::UserAgent , so you can still use any of LWP::UserAgent 's methods. WWW::Mechanize子类LWP::UserAgent ,因此您仍然可以使用LWP::UserAgent的任何方法。 Thus you can use the simple_request() method, which doesn't automatically handle redirects. 因此,您可以使用simple_request()方法,该方法不会自动处理重定向。 It just returns you the response as an HTTP::Resonse object. 它只是以HTTP::Resonse对象的形式返回响应。 Which means you can use the is_redirect() and header() methods to get the redirect URI. 这意味着您可以使用is_redirect()header()方法来获取重定向URI。 Which means you can then use the URI module to pull off everything after the #. 这意味着您可以使用URI模块提取#之后的所有内容。

Whew! 呼!

Your code would look something like this: 您的代码看起来像这样:

my $response = $mech->simple_request( HTTP::Request->new(GET => 'http://www.example.com/') );
if( $response->is_redirect ) {
  my $location = $response->header( "Location" );
  my $uri = new URI( $location );
  my $new_url = $uri->scheme . $uri->opaque;
# And here is where you do the load of the new URL.
}

There may be some twiddling to do, potentially around the header() line, but this would be the general idea. 可能有一些花哨的事情要做,可能在header()行周围,但这将是一般的想法。

WWW::Mechanize is a subclass of LWP::UserAgent , so the answer is the same. WWW :: MechanizeLWP :: UserAgent的子类,因此答案是相同的。

If you want to handle the redirect yourself to rewrite URLs, you might want to use a response_done or response_redirect handler. 如果要自己处理重定向以重写URL,则可能要使用response_doneresponse_redirect处理程序。 See the "Handlers" section of the LWP::UserAgent docs. 请参阅LWP :: UserAgent文档的“处理程序”部分。

As for "properly", the HTTP specification doesn't say what a client should do with a fragment except in 14.6 in the case of a referrer header (and that's the only place the word "fragment" even shows up). 至于“适当”, HTTP规范没有说明客户端应该如何处理片段,除非在引荐来源标头的情况下(在14.6中这是唯一出现“片段”的地方)。

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

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