我正在使用Net::Curl::Easy
perl,但无法获得正确的 http 响应。LWP::UserAgent
使用作品执行相同的请求。
真正的问题Net::Curl::Easy
是它无法创建正确的 http 请求。看起来它发送带有标头参数Expect: 100 continue的 http 请求的一部分,这就是服务器使用响应标头HTTP/1.1 100 Continue不正确数据进行响应的原因。
示例代码是:
#!/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_resp2和easy_resp2之间的区别。