Reading a web page into my CGI program

I once saw code to read a page at a URL into my CGI program. It was really short and simple and didn't involve loads of mucking around with sockets.

Any ideas?
Who is Participating?
prakashk021799Connect With a Mentor Commented:
use LWP::Simple;

$page = get("");
The above solution is the best one if you just want to get the contents of a web page into your program.

If you want to get into more complicated things, like examining the response code or headers etc., you need to use LWP::UserAgent, which gives you more control.

An example:

use LWP::UserAgent;

$ua = LWP::UserAgent->new;
$request = HTTP::Request->new('GET', '');
$response = $ua->request($request);

if ($response->is_success) {
    $page = $response->content;
} else {
    print $response->error_as_HTML;
rafisternAuthor Commented:
Your answer looks like it ought to work. However I can't seem to get it working, probably because I am working in an ASP environment using ActiveState Perl (when I said "CGI program", I was not being entirely accurate). I am going to have a go at making it work and in any case I will give you points. However I don't want to close the question right now in order that other people might comment.

ASP and CGI are mutually exclusive. A web server can support both ASP and CGI at the same time.

Also, the above has nothing in it that restricts its use only in a CGI environment. It will work in a CGI script, a normal script or an ASP script (I don't know if such a thing as 'ASP script' exists or not; I am not that well versed with the ASP terminology).
rafisternAuthor Commented:
Yes, I do know the difference between an ASP environment and an executable program or an interpretted script.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.