Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Finding a redirect's URL

by kbeen (Sexton)
on Nov 23, 2001 at 21:12 UTC ( #127144=perlquestion: print w/replies, xml ) Need Help??

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


I have been cracking my head against a wall trying to get the URL that a user is supposed to be redirectted to after submiting data.
After the data is submited the next page should contain a redirect, as the site administrator assured me that the asp code is this:


I read that the default for following redirects from a POST query is off, and I have tried it with both POST and GET. However, I don't want the script to actually follow the redirect, just get that precious "qid" value for me.

Using the code below, I can't find the redirect in the header or in the content. Does anybody know what is going on, and how my browser can be redirected, but there is actually information sent that tells it where to redirect to? Is there someway that I am not seeing every last byte of response using below?

my $hdrs = new HTTP::Headers('Cookie' => $cookie); my $linkinfo = qw( +pass); my $ncontent = new HTTP::Request('POST', $linkinfo, $hdrs); my $resp = $ua->request($content); my $body = $resp->content; my $string = $resp->as_string(); print $string; # no redirect in here print $body; #none here either
Any advice is appreciated.

Replies are listed 'Best First'.
Re: Finding a redirect's URL
by wog (Curate) on Nov 23, 2001 at 22:25 UTC
    LWP allows you to make a request without LWP following redirects, etc. with the simple_request method of LWP::UserAgent. For example:

    my $request = HTTP::Request->new("GET", ""); my $response = $ua->simple_request($request); print $response->header("Location"), "\n"; #

    Also the HTTP::Response class has a previous method to get the previous response in a chain of responses (as occurs when LWP follows redirects):

    my $request = HTTP::Request->new("GET", ""); my $response = $ua->request($request); print $response->previous->header("Location"), "\n"; #
Re: Finding a redirect's URL
by tachyon (Chancellor) on Nov 23, 2001 at 22:06 UTC
    use CGI; my $q = new CGI; print $q->redirect(""); # or roll it yourself print <<THIS Status: 302 Moved Location: http://blah.blah THIS




Re: Finding a redirect's URL
by staeryatz (Monk) on Nov 24, 2001 at 00:37 UTC
    There is a simple yet effective way of using redirection, without the need for a module.

    Use it just as you'd do an http header.
    # code to resolve what $url is going to be # ... # redirection print "Location: $url\n\n";
    'Location' is a standard CGI header, just like 'Content-type' and you'd use them the same way: make it the first thing your CGI script prints out, and make 2 newlines afterwards.

    This code is tested and works (providing you fill in the blanks). I used it for an anti-leech download page found here.
Re: Finding a redirect's URL
by kbeen (Sexton) on Nov 24, 2001 at 06:01 UTC
    I think maybe my question was a little unclear...

    I do not want to MAKE a redirect, rather find out where a page is trying to redirect the user.. Also, I don't really want to be redirected, only to get the URL that I would be redirected to if I was using a browser.

    Wog's comment seems like something I should look into, however, my problem is that LWP does not seem to be following a redirect. In fact, there does not seem to be a redirect in that response at all.

    The browser does redirect, and the site's administrator showed me the little bit of ASP code that actually creates the redirect.
    Where could this redirect be hiding? Am I missing something with LWP that is keeping me from seeing this?

      LWP comes with a nifty stand alone program called GET. GET is a script that uses the LWP modules to fetch remote web pages. The following worked for me:
      % GET -Sd GET --> 301 Moved Permanently GET --> 200 OK
      Which shows being redirected to This works fine if you just want to run a few URLs by hand. If you want to automate this functionality in a script, you can always peek under the covers of GET and emulate what its doing.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://127144]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2023-01-28 03:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found