Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Finding a redirect's URL

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

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

Hi,

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:

Response.Redirect("display.asp?qid=XXXXXX")

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(http://www.nottherealsite.com/login.asp?ui=kbeen&pw= +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.
Thanks
Kbeen

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", "http://cnn.com/europe"); my $response = $ua->simple_request($request); print $response->header("Location"), "\n"; # http://europe.cnn.com

    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", "http://cnn.com/europe"); my $response = $ua->request($request); print $response->previous->header("Location"), "\n"; # http://europe.cnn.com
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("http://somesite.somewhere.com?my_query=JaPh"); # or roll it yourself print <<THIS Status: 302 Moved Location: http://blah.blah THIS

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

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?

    Thanks,
    Kbeen
      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 http://www.slashdot.org GET http://www.slashdot.org --> 301 Moved Permanently GET http://slashdot.org/ --> 200 OK
      Which shows www.slashdot.org being redirected to slashdot.org. 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.

      -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found