English 中文(简体)
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.

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 subclasses LWP::UserAgent, so you can still use any of LWP::UserAgent s methods. Thus you can use the simple_request() method, which doesn t automatically handle redirects. It just returns you the response as an HTTP::Resonse object. Which means you can use the is_redirect() and header() methods to get the redirect URI. Which means you can then use the URI module to pull off everything after the #.

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.

WWW::Mechanize is a subclass of LWP::UserAgent, so the answer is the same.

If you want to handle the redirect yourself to rewrite URLs, you might want to use a response_done or response_redirect handler. See the "Handlers" section of the LWP::UserAgent docs.

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).





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签