使用perl脚本的HTTP Post在使用代理
时返回503服务不可用#!/usr/bin/perl -w
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout( 20 );
my $server_endpoint = "https://mytest.test.com/events";
$ua->proxy('https' , "http://1.2.3.4:8088");
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
$req->authorization_basic('user1', 'user1pass123!');
print "Posting URL: $server_endpoint \n";
# add POST data to HTTP request body
my $post_data = '{"events":[{"signature":"my_test_box:application:Network","source_id":"1.2.3.4","manager":"my_manager_srv","source":"my_test_box_2","class":"application","type":"Network","severity":3,"description":"high network utilization in application A"}]}';
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success)
{
my $message = $resp->decoded_content;
print "Received reply: $message\n";
print "HTTP POST code: ", $resp->code, "\n";
print "HTTP POST message: ", $resp->message, "\n";
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
任何帮助高度赞赏 感谢。
在此处添加命令curl和输出:
#curl -u user1:user1pass123! mytest.test.com/events -H "Content-Type: application/json" -X POST --data '{"events":[{"signature":"my_test_box:application:Network","source_id":"1.2.3.4","manager":"my_manager_srv","source":"my_test_box_2","class":"application","type":"Network","severity":3,"description":"high network utilization in application A"}]}'; -v -x http://1.2.3.4:8088
* About to connect() to proxy 1.2.3.4 port 8088 (#0)
* Trying 1.2.3.4... connected
* Connected to 1.2.3.4 (1.2.3.4) port 8088 (#0)
* Establish HTTP proxy tunnel to mytest.test.com:443
* Server auth using Basic with user 'user1'
> CONNECT mytest.test.com:443 HTTP/1.1
> Host: mytest.test.com:443
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Proxy-Connection: Keep-Alive
> Content-Type: application/json
> < HTTP/1.1 200 Connection established <
* Proxy replied OK to CONNECT request
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
* Server certificate:
* subject: CN=*.test.com,OU=Domain Control Validated
* start date: Nov 01 22:33:00 2017 GMT
* expire date: Nov 01 22:33:00 2018 GMT
* common name: .test.com
* issuer: CN=Go Daddy Secure Certificate Authority - G2,OU=certs.godaddy.com/repository/,O="GoDaddy.com, Inc.",L=Scottsdale,ST=Arizona,C=US
* Server auth using Basic with user 'user1' > POST /events HTTP/1.1
> Authorization: Basic xxxxxxxxxxxxxxxx
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: mytest.test.com
> Accept: */
> Content-Type: application/json
> Content-Length: 325
> < HTTP/1.1 200 OK < Date: Fri, 19 Jan 2018 08:38:23 GMT < Content-Length: 101 < Connection: keep-alive
< Server: nginx
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=63072000; includeSubDomains; always
< {"response"{"processed":1,"cached":0,"received":1},"success":true,"message":"Processed 1 event(s)"}
* Connection #0 to host 1.2.3.4 left intact
* Closing connection #0
答案 0 :(得分:0)
由于您使用的Perl版本相当旧,因此无法以这种方式实际设置HTTPS代理。你被迫通过环境变量做低级别。
manifest.json
请注意,必须在之前创建UserAgent。
除此之外,您应该导入SSL连接的模块$ENV{HTTPS_PROXY}='http://1.2.3.4:8088';
(IO::Socket::SSL
是首选的,但对于旧版本的Perl来说,它最明显不可用。您可以导入模块设置调试级别(从1到4,我建议3)。
总结一下,这应该是代码的开头。
Net::SSL
P.S。:你还应该检查你用Perl获得的响应头,而不是curl。如果#!/usr/bin/perl -w
use LWP::UserAgent;
use IO::Socket::SSL qw(debug3);
$ENV{HTTPS_PROXY}='http://1.2.3.4:8088';
$ENV{HTTPS_DEBUG}=1;
my $ua = LWP::UserAgent->new;
...
为false,请添加print $resp->headers_as_string();
。
答案 1 :(得分:-1)
很难分析问题,因为您使用的是9岁(和过时的)软件堆栈。但是考虑到您在评论中显示的响应,我怀疑您的代码不会出现SSL问题但您的代理有问题。要引用HTTP响应主体:
SSL证书验证错误(ssl_failed)...
这通常是由未配置为接受SSL连接的Web站点引起的,但可能是因为Proxy不信任Web站点的证书颁发机构。
鉴于您对LWP存在此问题但没有卷曲此响应可能是由于这些客户端如何构造对代理的请求的差异。如果您的设置使用IO :: Socket :: SSL作为后端,则旧版本的LWP将无法构建可能导致此故障的正确代理请求(在2014年的LWP 6.06中已修复)。如果您使用Crypt :: SSLeay而不是在这个旧版本中缺少对SNI的支持可能是问题(在2012年修复为0.59_3)。
无论如何,我真的建议远离旧的,不受支持的,可能不安全的软件堆栈。如果使用当前堆栈,问题可能会消失。