LWP request example

Shows how to load a text file <br/> <br/> Another good resource: Web Client Programming with Perl (oreilly.com).
# load LWP library: use LWP::UserAgent; use HTML::Parse; # define a URL my $url = 'http://www.jonasjohn.de'; # create UserAgent object my $ua = new LWP::UserAgent; # set a user agent (browser-id) # $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)'); # timeout: $ua->timeout(15); # proceed the request: my $request = HTTP::Request->new('GET'); $request->url($url); my $response = $ua->request($request); # # responses: # # response code (like 200, 404, etc) my $code = $response->code; # headers (Server: Apache, Content-Type: text/html, ...) my $headers = $response->headers_as_string; # HTML body: my $body = $response->content; # print the website content: # print $body; # do some parsing: my $parsed_html = HTML::Parse::parse_html($body); for (@{ $parsed_html->extract_links(qw(a body img)) }) { # extract all links (a, body, img) my ($link) = @$_; # print link: print $link . "n"; }

Url: http://www.jonasjohn.de/snippets/perl/lwp-example.htm

Language: Perl | User: ShareMySnippets | Created: Oct 16, 2013