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?
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).
I am building a Web interface to monitor an embedded system. I have built a Perl script which runs remote commands and gathers output from that system. Now what I need is a Web interface which makes ...
How do I tell what type of value is in a Perl variable? $x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).
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: "...
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 ...
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 ...
I would like to submit a form to a CGI script localy (w3c-markup-validator), but it is too slow using curl and apache, I want to use this CGI script more than 5,000 times in an another script. and ...
So I m running perl 5.10 on a core 2 duo macbook pro compiled with threading support: usethreads=define, useithreads=define. I ve got a simple script to read 4 gzipped files containing aroud 750000 ...
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 ...