How to test an HTTP header value using LWP::UserAgent

China☆狼群 提交于 2019-12-11 09:39:42

问题


I may be posting more questions as I get answers, but here goes!

I am currently trying to troubleshoot a Perl script that my university used to use to automatically download files with SAT score data.

The idea is to read through emails in a certain account; pull out the cycle number (which is used in the URL); piece together multiple URLs; and then use LWP::UserAgent to grab the files from the server and do other Perl magicks on them.

In my investigation I have determined that manually entering the URL (and thus limiting the script to be run by the user, with the cycle number being replaced each time) actually works.

In examining the response objects sent back I noticed (first of all) that the following line is missing from the request that fails:

'_uri_canonical' => $VAR1->{'_request'}{'_uri'}

However it is present in the request that succeeds.

If any of you can tell me why this line is missing in the unsuccessful request you will have my thanks, but that's not what I'm asking about.

My question pertains to further investigation to see why it's being rejected.

In the documentation for LWP::UserAgent I noticed this:

Error responses that LWP generates internally will have the "Client-Warning" header set to the value "Internal response". If you need to differentiate these internal responses from responses that a remote server actually generates, you need to test this header value.

My question: how do you actually test that header value? (Pardon any ignorance; I'm an intern in my college's IT department)


回答1:


It actually places a variety of messages in that header, which you can obtain as follows:

if (my $cw = $response->header('Client-Warning')) {
   die("Internal error: $cw\n");
}

The Internal response message is only used when the status already indicates an error. Other messages such as Redirect loop detected and Missing Authenticate header are used at other times. The above code will get whatever message is present there rather than just checking for Internal response.




回答2:


The standard way of using LWP is something like

use strict;
use warnings;

use LWP;

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

my $resp = $ua->get('http://my.domain.co.uk/');

die $resp->status_line unless $resp->is_success;

my $content = $resp->decoded_content;

What do you need to know?



来源:https://stackoverflow.com/questions/15280434/how-to-test-an-http-header-value-using-lwpuseragent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!