FloydATC has asked for the wisdom of the Perl Monks concerning the following question:
The web server requires a username and password, upon successful authentication it responds with a 302 redirect to the requested URI, like so:#!/usr/bin/perl use strict; use warnings; use LWP; my $path = "Infrastruktur/Svitsjoversikt"; my $loginurl = "http://intranett.oikt.local/user/login"; my $finaluri = "/Infrastruktur/Svitsjoversikt"; my $user = "foo"; my $pass = "bar"; my $param = "Login=$user&Password=$pass&LoginButton=1&RedirectURI=$fin +aluri"; # Create a user agent my $ua = LWP::UserAgent->new; $ua->agent("monitorezex/1.0 "); #push @{ $ua->requests_redirectable }, 'POST'; # Make POST requests +redirectable $ua->cookie_jar({ file => "cookies.txt" }); # Create a request my $req = HTTP::Request->new( POST => $loginurl ); $req->content_type('application/x-www-form-urlencoded'); $req->content( $param ); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print "SUCCESS!\n"; print $res->as_string; } else { print "FAILURE: " . $res->status_line . "\n"; print $res->as_string; } exit;
Notice that the login is successful, I get a cookie and a redirect URL. For some reason, if I uncomment the line to enable automatic redirection, the web server gets a malformed URL from my script:HTTP/1.1 302 Found Cache-Control: no-cache, must-revalidate Connection: close Date: Fri, 09 Oct 2009 06:45:56 GMT Pragma: no-cache Location: http://intranett.oikt.local/Infrastruktur/Svitsjoversikt Location: http://intranett.oikt.local/Infrastruktur/Svitsjoversikt Server: Apache/1.3.39 (Win32) PHP/5.2.6 Content-Language: no-bokmaal Content-Type: text/html; charset=utf-8 Expires: Mon, 26 Jul 1997 05:00:00 GMT Last-Modified: Fri, 09 Oct 2009 06:45:56 GMT Client-Date: Fri, 09 Oct 2009 06:45:56 GMT Client-Peer: 10.10.8.120:80 Client-Response-Num: 1 Client-Transfer-Encoding: chunked Refresh: 0;URL=http://intranett.oikt.local/Infrastruktur/Svitsjoversik +t Served-By: intranett.oikt.local Set-Cookie: eZSESSIDoikt_intranett=v8igadrdc86koc225jk55or5e2; path=/ X-Powered-By: eZ Publish <HTML><HEAD><META HTTP-EQUIV="Refresh" Content="0;URL=http://intranett +.oikt.local/Infrastruktur/Svitsjoversikt"><META HTTP-EQUIV="Location" + Content="http://intranett.oikt.local/Infrastruktur/Svitsjoversikt">< +/HEAD><BODY></BODY></HTML>
I understand that this problem can be solved by fetching the new URL manually, but can someone point out why the automatic redirect doesn't work as I expect?10.15.0.2 - - [09/Oct/2009:08:38:31 +0200] "POST /user/login HTTP/1.1" + 302 246 10.15.0.2 - - [09/Oct/2009:08:38:32 +0200] "GET /Infrastruktur/Svitsjo +versikt,%20http://intranett.oikt.local/Infrastruktur/Svitsjoversikt H +TTP/1.1" 404 23015
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: LWP redirect problem, produces malformed URL?
by gmargo (Hermit) on Oct 09, 2009 at 19:44 UTC | |
|
Re: LWP redirect problem, produces malformed URL?
by Anonymous Monk on Oct 09, 2009 at 08:36 UTC | |
by FloydATC (Deacon) on Oct 09, 2009 at 09:13 UTC | |
|
Re: LWP redirect problem, produces malformed URL?
by Gangabass (Vicar) on Oct 09, 2009 at 08:35 UTC |