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

How do I programatically follow redirects?

( #90794=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie bwarn on Jun 22, 2001 at 21:26 UTC
Q&A  > CGI programming


Description:

I'm checking to see if a site exists (I'm using LWP::UserAgent), is up, etc. before directing a user to a site. Unfortunately, if the destination site itself redirects users with a query (i.e., uses ? in the URL), my program bombs.

Does anyone have any suggestions how to successfully follow redirects?

-- Code Snippet --
$ua = new LWP::UserAgent; $request = new HTTP::Request('HEAD', "$URLVAL{URL}"); ($response) = $ua->request($request); if ($response->is_success){ print "Location:$URLVAL{URL}\n\n"; # quits if destination site redir +ects using '?' [or encoded equivalent] in its redirection script } else { # send user to another cgi that displays some 'failed URL' text +.}

Answer: How do I programatically follow redirects?
contributed by gunder

I would maybe try a different approach by accessing site information via TCP sockets? Using (use Socket) you cn verify an alive site by grabing some info from the page if you like... hell I have never tried this, but it should work right? Let me know if it does...

There is a wrapped up IO class called IO::Socket::INET that should for websites...

try

$client = IO::Socket::INET->new("PeerAddr => "the.host.com", PeerPort +=> 80, Type => SOCK_STREAM, Timeout => (How ever long you think it sh +ould wait for a reply)) or die $@;
Then (in case you want to get some data from the connected socket)
bind(SOCKET, $client) or die "bind: $!";
if you get this far without error, then the site should be up and you can read from that socket and should be able to pick out bytes of the HTML markup... I think...

Read more on sockets to get ideas on how to read via $client->recv($data_read, $flags) or die...

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2014-08-01 05:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (256 votes), past polls