Contributed by Zombie bwarn on Jun 22, 2001 at 21:26 UTC
Q&A  > CGI programming


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...


$client = IO::Socket::INET->new("PeerAddr => "", 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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.