【发布时间】:2016-10-31 16:55:27
【问题描述】:
我在向 HotelsPro 网络服务发送 Restful 请求时遇到问题。
我正在尝试使用基本凭据向此链接 https://api-test.hotelspro.com:443 发送请求,但每次“未提供身份验证凭据”时都会出错,尽管此凭据在浏览器上运行。我的代码如下所示
sub getJSONdata {
my ($SupplierXMLServer, $message, $compressed,$timeOut) = ();
($SupplierXMLServer, $message, $compressed,$timeOut) = @_;
$SupplierXMLServer='https://api-test.hotelspro.com/api/v2/search/?destination_code=20b05&checkin=2016-11-09&checkout=2016-11-12¤cy=USD&client_nationality=PS&pax=2';
my $username = "Epilgrim";
my $password = "xxxxxxxxxx";
use LWP::UserAgent;
my $userAgent = LWP::UserAgent->new(agent =>"1");
$userAgent->credentials('https://api-test.hotelspro.com:443', 'api', $username , $password);
$userAgent->timeout($timeOut) if($timeOut); # in seconds
use HTTP::Request::Common;
my $response = '';
if($compressed){
$response = $userAgent->request( GET $SupplierXMLServer,
Content_Type => 'application/json',
Accept_Encoding => "gzip,deflate",
Content => $message);
}
else{
$response = $userAgent->request( GET $SupplierXMLServer,
Content_Type => 'application/json',
Content => $message);
}
return $response->error_as_HTML unless $response->is_success;
#return $response->content;
if($compressed){
return $response->decoded_content;
}
else{
return $response->content;
}
}
请帮助我编写正确的代码并以正确的方式发送请求以获得有效的响应。
【问题讨论】:
-
在您对
credentials的调用中,您可以尝试删除协议吗?它的格式应该是hostname:port- 即'api-test.hotelspro.com:443'请参阅stackoverflow.com/questions/1799147/…
标签: perl web-services credentials restful-url lwp-useragent