facebook twitter hatena line google mixi email

Perl/perlのLWPについてのメモ

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

LWPでのgetの仕方

#!/usr/bin/perl

print "Content-type: text/html\n\n";

use LWP::Simple;
$doc = get('http://www.yahoo.co.jp');
print $doc;

require LWP::UserAgent;
$ua = LWP::UserAgent->new;

$request = HTTP::Request->new('GET' => 'http://www.yahoo.co.jp');
$response = $ua->request($request); # or

use Data::Dumper;
print Dumper($response);
print $response->{_content};

LWPでのpostの仕方

use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
# リクエストの生成
my $url      = 'http://www.yahoo.co.jp';
my %formdata = ('user' => 'hoge', 'password' => 'piyo');
my $request  = POST($url, [%formdata]);
# UserAgentを生成して処理
my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);
%a = %$response;
if ($response->{_rc} == 200) {
  print $response->{_content};
}