检查来自 LWP::UserAgent 响应的响应状态
check response status from LWP::UserAgent response
LWP::UserAgent 使用我们将请求发送到端点,端点将响应发送为 HTTP/1.1 200 OK
或 HTTP/1.1 200 200
。我使用模式匹配来检查响应状态。
但我想使用一些通用函数,我们可以从中检查响应状态,而不是在 HTTP STATUS 中进行模式匹配。下面是我的代码。
use LWP::UserAgent;
my $ua = LWP::UserAgent->new( );
$ua->timeout(30);
my $req = HTTP::Request->new(POST => "$EndPoint");
$req->content_type('text/xml');
$req->content($send_data);
my $response = $ua->request($req);
my $http_status = '';
if ( $response =~ m/^HTTP.*?\s(.*)/ ) {
$http_status = uc(); # It is probably already upper case -- just making sure.
}
if ( $http_status =~ m/200/ ) {
if ( $response =~ m/Status=\"Successful\"/ ) {
print "Successful\n";
} else {
my $error_code = '';
if ( $response =~ m/Error Code=\"(.+?)\"/ ) {
print "Error: \n";
}
}
我尝试使用以下代码检查状态,但出现错误 Can't locate object method "is_success" via package "HTTP/1.1 200 OK
if($response->is_success){
print Dumper($response);
} else {
print Dumper($response->status_line);
}
编辑
perl send_request.pl
Can't locate object method "is_success" via package "HTTP/1.1 200 OK
##我在 $response
上添加了打印
Connection: close
Date: Mon, 10 Jan 2022 17:36:58 GMT
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Server: Apache
Vary: Accept-Encoding
Content-Length: 356
Content-Type: text/xml;charset=utf-8
Client-Date: Mon, 10 Jan 2022 17:36:58 GMT
Client-Peer: XXXXX
Client-Response-Num: 1
SOAPAction: ""
不要将响应字符串化,然后使用模式匹配。请改用提供的方法。
检查 2xx 代码:
if ( $response->is_success ) { ... }
要专门检查 200(在极少数情况下你想区分 200 OK
和 201 Created
),
if ( $response->code == 200 ) { ... }
如果你想要后面的字符串,可以使用
$response->status_line # "200 OK" or "200 200"
有查询响应的方法,你很少需要解析它的消息。
一个LWP::UserAgent::get
returns一个HTTP::Response对象。在其文档中我们发现
is_success
、is_info
、is_error
、is_client_error
、is_server_error
、is_redirect
最常用的是is_success
,大纲中有例子。
中关于这些含义的更多详细信息
LWP::UserAgent 使用我们将请求发送到端点,端点将响应发送为 HTTP/1.1 200 OK
或 HTTP/1.1 200 200
。我使用模式匹配来检查响应状态。
但我想使用一些通用函数,我们可以从中检查响应状态,而不是在 HTTP STATUS 中进行模式匹配。下面是我的代码。
use LWP::UserAgent;
my $ua = LWP::UserAgent->new( );
$ua->timeout(30);
my $req = HTTP::Request->new(POST => "$EndPoint");
$req->content_type('text/xml');
$req->content($send_data);
my $response = $ua->request($req);
my $http_status = '';
if ( $response =~ m/^HTTP.*?\s(.*)/ ) {
$http_status = uc(); # It is probably already upper case -- just making sure.
}
if ( $http_status =~ m/200/ ) {
if ( $response =~ m/Status=\"Successful\"/ ) {
print "Successful\n";
} else {
my $error_code = '';
if ( $response =~ m/Error Code=\"(.+?)\"/ ) {
print "Error: \n";
}
}
我尝试使用以下代码检查状态,但出现错误 Can't locate object method "is_success" via package "HTTP/1.1 200 OK
if($response->is_success){
print Dumper($response);
} else {
print Dumper($response->status_line);
}
编辑
perl send_request.pl
Can't locate object method "is_success" via package "HTTP/1.1 200 OK
##我在 $response
上添加了打印 Connection: close
Date: Mon, 10 Jan 2022 17:36:58 GMT
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Server: Apache
Vary: Accept-Encoding
Content-Length: 356
Content-Type: text/xml;charset=utf-8
Client-Date: Mon, 10 Jan 2022 17:36:58 GMT
Client-Peer: XXXXX
Client-Response-Num: 1
SOAPAction: ""
不要将响应字符串化,然后使用模式匹配。请改用提供的方法。
检查 2xx 代码:
if ( $response->is_success ) { ... }
要专门检查 200(在极少数情况下你想区分 200 OK
和 201 Created
),
if ( $response->code == 200 ) { ... }
如果你想要后面的字符串,可以使用
$response->status_line # "200 OK" or "200 200"
有查询响应的方法,你很少需要解析它的消息。
一个LWP::UserAgent::get
returns一个HTTP::Response对象。在其文档中我们发现
is_success
、is_info
、is_error
、is_client_error
、is_server_error
、is_redirect
最常用的是is_success
,大纲中有例子。