1

Net::Curl::Easyperl で使用していますが、適切な http 応答を取得できません。LWP::UserAgent作品を使用して同じ要求を行う。

の本当の問題Net::Curl::Easyは、適切な http 要求を作成できないことです。HTTP リクエストの一部をヘッダー パラメータExpect: 100 continueで送信しているように見えるため、サーバーはレスポンス ヘッダーHTTP/1.1 100 Continue illegal dataで応答します。

サンプルコードは次のとおりです。

#!/usr/bin/perl

use strict;
use warnings;
use Net::Curl::Easy;
use HTTP::Request::Common qw(GET POST);
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Cookies;

my $tmp;

my ($timeout, $handled_ref, $resend);

my $request;
my $url = "http://www.wegolo.com/";
$request = GET($url);
$request->header('User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041204 Firefox/1.0 (Debian package 1.0.x.2-1)');

#------ Using Net::Curl::Easy ------#
my $uri = $request->url()->as_string();

my $easy = Net::Curl::Easy->new({
        request     => $request,
        body        => '',
        headers     => '',
        timeout     => $timeout,
        handled_ref => $handled_ref, 
        resend      => $resend,
    });

$easy->setopt( Net::Curl::Easy::CURLOPT_URL(),         $uri );
$easy->setopt( Net::Curl::Easy::CURLOPT_WRITEHEADER(), \$easy->{headers} );
$easy->setopt( Net::Curl::Easy::CURLOPT_FILE(),        \$easy->{body} );

$easy->perform();

open $tmp, ">", "easy_resp1";
print $tmp Dumper $easy->{headers};
print $tmp "\n\n\n\n";
print $tmp Dumper $easy->{body};
close $tmp;

#------ Using LWP::UserAgent ------#
my $ua = LWP::UserAgent->new();
my $cookie_jar = HTTP::Cookies->new();
$ua->cookie_jar($cookie_jar);
$ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041204 Firefox/1.0 (Debian package 1.0.x.2-1)');
my $response = $ua->request($request);

open $tmp, ">", "ua_resp1";
print $tmp Dumper $response;
close $tmp;


my @cookie_string = $easy->{headers} =~ /Set\-Cookie\:\s+(.+)/gxi;
my $cookie_string = "";

foreach my $cookie (@cookie_string) {
    my @temp = split(";", $cookie);
    $cookie_string .= $temp[0]."; ";
}
$cookie_string =~ s/^\s+//gxi;
$cookie_string =~ s/\s+$//gxi;

my $dep_country_code = 'CA';
my $dep_country_name = 'Canada';
my $dep_city         = 'Toronto';

my ($MyScriptManager_HiddenField) = $easy->{body} =~ /\_TSM\_CombinedScripts\_=([^"]*)\"/x;
my ($viewState)                   = $easy->{body} =~ /id="__VIEWSTATE"\s+value="([^"]*)"/gx;
my ($eventValid)                  = $easy->{body} =~ /id="__EVENTVALIDATION"\s+value="([^"]*)"/gx;
my ($tbDepartureDate)             = $easy->{body} =~ /value="([A-Za-z\d\,\s]+)"\s+id="tbDepartureDate"/gx;
my ($tbReturnDate)                = $easy->{body} =~ /value="([A-Za-z\d\,\s]+)"\s+id="tbReturnDate"/gx;


my $form_elements = {
            'MyScriptManager'             => 'MyScriptManager|ddlCountry',
            'MyScriptManager_HiddenField' => $MyScriptManager_HiddenField,
            '__EVENTTARGET'               => 'ddlCountry',
            '__EVENTARGUMENT'             => '',
            '__LASTFOCUS'                 => '',
            '__VIEWSTATE'                 => $viewState,
            '__EVENTVALIDATION'           => $eventValid,
            'ddlCountry'                  => $dep_country_code,
            'TripType'                    => 'false',
            'ddlDeparture'                => '',
            'tbDepartureDate'             => $tbDepartureDate,
            'ddlDestination'              => '',
            'tbReturnDate'                => $tbReturnDate,
            'ddlNumAdults'                => 1,
            'ddlNumChildren'              => 0,
            'ddlNumInfants'               => 0,
            'ddlCurrency'                 => 'CAD',
            'deepLinkUrl'                 => '',
            '__ASYNCPOST'                 => 'true',
            ''                            => '',
        };



$request = POST $url, $form_elements;
$request->header('User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041204 Firefox/1.0 (Debian package 1.0.x.2-1)');


#------ Using Net::Curl::Easy ------#
    $uri = $request->url()->as_string();
    my $content = $request->content();

    $easy = Net::Curl::Easy->new({
            request     => $request,
            body        => '',
            headers     => '',
            timeout     => $timeout,
            handled_ref => $handled_ref, 
            resend      => $resend,
        });

    $easy->setopt( Net::Curl::Easy::CURLOPT_URL(),         $uri );
    $easy->setopt( Net::Curl::Easy::CURLOPT_POSTFIELDS,    $content );
    $easy->setopt( Net::Curl::Easy::CURLOPT_POSTFIELDSIZE, length($content) );
    $easy->setopt( Net::Curl::Easy::CURLOPT_COOKIE,        $cookie_string );
    $easy->setopt( Net::Curl::Easy::CURLOPT_WRITEHEADER(), \$easy->{headers} );
    $easy->setopt( Net::Curl::Easy::CURLOPT_FILE(),        \$easy->{body} );

    $easy->perform();

    open $tmp, ">", "easy_resp2";
    print $tmp Dumper $easy->{headers};
    print $tmp "\n\n\n\n";
    print $tmp Dumper $easy->{body};
    close $tmp;
#-----------------------------------#



#------ Using LWP::UserAgent ------#
    $response = $ua->request($request);

    open $tmp, ">", "ua_resp2";
    print $tmp Dumper $response;
    close $tmp;
#----------------------------------#

このファイルを実行した後、 ua_resp2easy_resp2の違いを確認します。

4

1 に答える 1