作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新的 perl 程序员,试图使用 LWP:UserAgent 将 curl 请求转换为 Perl 脚本。
curl请求示例是:
curl -X GET -H "Authorization: Basic YWRtaW46YWRtaW4=" -H "Cache-Control: no-cache" -H "Postman-Token: eb3955f1-a7b5-65d7-f5c0-808c7aba6cef" "https://10.51.10.26/10/download?startTime=1461698250&endTime=1461698252&cNat=True&cNatShowDst=True&tuplesFile=True&summarizeTuples=False"
use LWP::UserAgent;
my $browser = LWP::UserAgent->new;
my $url = 'https://10.51.10.26/10/download';
my @headers = (
"startTime" => $queryStart,
"endTime" => $queryEnd,
"cNat" => "True",
"cNatShowDst" => "False",
"tuplesFile" => "False",
"summarizeTuples" => "False",
"Authorization" => "Basic YWRtaW46YWRtaW4",
"Cache-Control" => "no-cache",
"Postman-Token" => "eb3955f1-a7b5-65d7-f5c0-808c7aba6cef",
);
HTTP::Response=HASH(0x27884bc)
最佳答案
如果您想使用 LWP::UserAgent 使用自定义 header 进行 GET 请求,您可以将它们放入 $ua->get()
顺便来电the documentation describes .
This method will dispatch a GET request on the given $url. Further arguments can be given to initialize the headers of the request. These are given as separate name/value pairs. The return value is a response object. See HTTP::Response for a description of the interface it provides.
@headers
数组包含 header 和 URL 参数。这不会如你所愿。如果您想像这样构造 URL 和 header ,则需要一种不同的方法。
get
发送它,包括标题。
use strict;
use warnings;
use LWP::UserAgent;
use URI;
my $uri = URI->new('https://10.51.10.26/10/download');
$uri->query_form(
"startTime" => $queryStart, # these two need
"endTime" => $queryEnd, # to be set above
"cNat" => "True",
"cNatShowDst" => "False",
"tuplesFile" => "False",
"summarizeTuples" => "False",
);
my $ua = LWP::UserAgent->new;
my $res = $ua->get(
$uri,
"Authorization" => "Basic YWRtaW46YWRtaW4",
"Cache-Control" => "no-cache",
"Postman-Token" => "eb3955f1-a7b5-65d7-f5c0-808c7aba6cef",
);
if ($res->is_success) {
# do stuff with content
} else {
# request failed
}
use Data::Dumper;
print Dumper $res;
关于Perl LWP :UserAgent how to I add headers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609198/
我是一名优秀的程序员,十分优秀!