dibucho has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks, still beginner in Perl, hope you can help me to solve this problem soon. I just try to make a get on a page which is redirected. But somehow the redirection doesn't work. The content of the respond is the following:
<HTML> <!-- File: redirectmeta.html --> <HEAD> <LINK REL="stylesheet" TYPE="text/css" HREF="/livelink_desupport/livel +ink.css"> <STYLE> .browseRow1 { background-color: #FFFFFF; } .browseRow2 { background-color: #EEEEEE; } </STYLE> <TITLE>Livelink - Umleitung</TITLE> <META HTTP-EQUIV="Refresh" CONTENT="0; URL=/livelink_de/livelink/fetch +/2000/13022444/customview.html?func=ll&amp;objId=13022444&amp;objActi +o n=browse&amp;sort=name"> </HEAD> </HTML> <!-- End File: redirectmeta.html -->
It does not matter if I use the link directly, it gives me the same result. Here is my code:
use LWP::UserAgent; use HTTP::Cookies::Netscape; $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); my $cookie_jar = HTTP::Cookies::Netscape->new( file => "C:/Mozilla/Firefox/Profiles/096xdbsa.default/cookie +s.txt", autosave => 1, ); $ua->cookie_jar( $cookie_jar ); my $res = $ua->get('https://tsi-myworkroom-de.telekom.de/livelink_de +/livelink/fetch/2000/13022444/customview%2Ehtml?func=ll&objId=1302244 +4&objAction=browse&sort=name'); if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }
Thanks for all your help Greetings Dibucho

Replies are listed 'Best First'.
Re: LWP::UserAgent, redirection does not work
by Corion (Patriarch) on Oct 08, 2007 at 14:25 UTC

    LWP::UserAgent does not interpret META tags like your browser does. Also, it does not follow 30x-style redirects unless you tell it to.

    You don't show us what result code the first ->get() returns. Maybe it's 200, maybe it's 301 or 302. You need to react to those accordingly - either by parsing the returned HTML page for META tags, or by telling LWP::UserAgent to follow the redirect.

    Also, if you're automating a website, WWW::Mechanize acts more like a browser than LWP::UserAgent does.

      Also, it does not follow 30x-style redirects unless you tell it to

      It's true that it won't parse the HTML response to look for and interpret META elements, but it does handle 3xx reponses automatically (for GET and HEAD queries by default).

      use LWP::UserAgent qw( ); my $response = LWP::UserAgent->new()->get('http://www.w3.org/html'); for (;;) { my $uri = $response->request()->uri(); my $status = $response->status_line(); print("$uri => $status\n"); $response = $response->previous(); last if !defined($response); print("as a result of\n"); }
      http://www.w3.org/html/ => 200 OK as a result of http://www.w3.org/html => 301 Moved Permanently
      Thanks for your reply, I just checked the result code of the first get(), it's 200. But something is strange with the redirection link,it seems like an infinitive redirection cycle, because the site redirects on itself. But if I use a browser like Firefox or IE the redirection works fine. Any idea? bye dibucho

        As always when automating things and finding different behaviour between other programs, you need to find out where exactly the differences lie. My first guess would be that you're not sending the expected Referer (sic) header. But only a network trace like the HTTP Live Headers or Wireshark produce can tell for sure.

        Maybe WWW::Mechanize already works for you as it mimics browser behaviour far closer than LWP::UserAgent does of itself.