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

I am trying to retrieve a webpage using LWP over an SSL connection. I am getting a 302 error and being returned the page to be redirected to in the header('Location').

The page to be redirected to is a dynamic page and when I try to retrieve it, it is gone as well.

I am using ActiveState Perl 5.6.1 build 626 on Win NT 4.0. I had to install Net::SSLeay by hand from cpan. I have OpenSSL v0.9.6b9 installed.

Any ideas? I have attached the code and output below. I removed the code that tried to retrieve the missing page. Figured it may be better to go from here.
Here is the code
#!/usr/bin/perl -w use strict; use LWP::UserAgent; use LWP::Protocol::https; use URI::URL; my %parm = ( url => 'https://XXX.XXX.XXX.XXX', uatimeout => 120, browser => 'IE/5.0', username => 'XXXXX', password => 'XXXXX', realm => 'XXXXX', ); my $netloc = new URI::URL($parm{'url'})->netloc(); my $ua = new LWP::UserAgent; $ua->agent($parm{'browser'}); $ua->timeout($parm{'timeout'}); $ua->credentials($netloc, $parm{'realm'}, $parm{'username'}, $parm{'password'}); my $request = new HTTP::Request POST=> $parm{'url'}; $request->authorization_basic($parm{'username'}, $parm{'password'}); my $page = $ua->request($request); #print $request->as_string(); if ($page->is_success) { print "Redirect\n"; print $page->content; } elsif ($page->is_redirect) { print $page->content; } else { print $page->error_as_HTML; print $page->content; }
Here is the output
C:\TEMP>perl -w bcbs.pl Redirect <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>302 Found</TITLE> </HEAD><BODY> <H1>Found</H1> The document has moved <A HREF="https://XXX.XXX.XXX.XXX:443/?&amp;STCO +=1O3AEz6wYA2QAACjwXZg&amp;STCOEND">here</A>.<P> <P>Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle t +he request . </BODY></HTML>


seanbo

Replies are listed 'Best First'.
Re: LWP, SSL, 302
by seanbo (Chaplain) on Aug 07, 2001 at 20:42 UTC
    Woops....the code should have been this...
    if ($page->is_success) { print $page->content; } elsif ($page->is_redirect) { print "Redirect\n"; print $page->content; } else { print $page->error_as_HTML; print $page->content; }
    P.S. - I pasted in the wrong code..

    seanbo
      Maybe I'm not understanding, but it sounds like there's a 302 loop. What happens when you do this in a browser instead of in the program? Do you get to the right location, or do you keep getting 302s?

      I noticed in your code that you don't seem to follow the redirect, but you mention that when you do, it doesn't work. Could you post that code?

        You hit the nail on the head...a 302 loop. The strange thing is, in a browser, it works fine, I just get redirected to the proper page.

        I am going to dig out the code that tried to follow the rediret and post it.

        seanbo
Re: LWP, SSL, 302
by mitd (Curate) on Aug 08, 2001 at 02:49 UTC

    LWP::UserAgent::request(...) 'usually' performs redirect 'automagically' except when request method is 'POST'.
    Try sub-classing LWP::UserAgent and over riding the
    redirect_ok method as suggested in docs.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Here is some ancient code of mine that did exactly that, except the purpose was to *disallow* all redirects, including GETs. Its probably just a simple tweak to reverse the logic, and allow redirects on everything including POSTs. Anyway, here is the code:

      # file: # /usr/local/perllibs/LWP/UserAgent/NoRedirects.pm # usage: # use LWP::NoRedirects; # use LWP::UserAgent; package LWP::UserAgent::NoRedirects; use base qw(LWP::UserAgent); sub new { my ($class, $base) = @_; my $parser = new LWP::UserAgent; $parser->{base} = $base; bless $parser, $class; } sub redirect_ok { return 0; } 1;

      -Blake

        I tried sub-classing LWP::UserAgent and overriding the redirect_ok method to return one everytime as blakem did in his code (but opposite) here.
        #I changed the return value sub redirect_ok { return 1; } #Now I get no page returned.


        seanbo
        OK. Here is what I did to force the redirect on the POST.
        package LWP::UserAgent::ForceRedirects; use LWP::UserAgent; use base qw(LWP::UserAgent); sub new { my ($class, $base) = @_; my $parser = new LWP::UserAgent; $parser->{base} = $base; bless $parser, $class; } sub redirect_ok { my ($self, $request) = @_; print "Redirecting to: " . $request->url() . "...\n"; return 1; } 1;
        here is the output
        C:\TEMP>perl -w bcbs.pl Redirecting to: https://XXX.XXX.XXX.XXX/?&STCO=1O3F9cqwYA2QAAB6EH0k&ST +COEND... Redirecting to: https://XXX.XXX.XXX.XXX/?&STCO=2O3F9cqwYA2QAAB6EH0k&ST +COEND...


        seanbo
      'GET' returns nothing. Only invalid memory refernce errors at the OS level. (not sure why that happens)

      I am going to try to sub-class LWP::UserAgent. blakem, has provided me a starting point here.

      seanbo