I'm trying to write a tiny script to login and extract some information from our intranet server running eZ publish, by the use of LWP.
#!/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;
The web server requires a username and password, upon successful authentication it responds with a 302 redirect to the requested URI, like so:
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>
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:
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
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?

-- Time flies when you don't know what you're doing

In reply to LWP redirect problem, produces malformed URL? by FloydATC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.