use URI::Escape;#URL编码
use JSON;
#发送GET请求
use LWP::Simple;
my $tmp = "您本次操作的验证码为:$code";
my $smsmsg = uri_escape_utf8($tmp); #URL编码
my $args="http://xxx?p=".$phone."&c=$smsmsg";
my $response = get($args);
或者:
use HTTP::Request;
use HTTP::Headers;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10); #设置超时时间10s, 默认是180s
my $header = HTTP::Headers->new( Content_Type => 'text/html; charset=utf8', ); #设置head
$request = HTTP::Request->new('GET', $url, $header);
my $response = $ua->request($request);
my $res = "";
if ($response->message ne "OK" && $response->is_success ne "1") { #出错,或者timeout了
$res = "error or timeout";
} else {
$res = decode("utf-8",$response->content);
}
#发送POST请求
use HTTP::Request;
use HTTP::Headers;
use LWP::UserAgent;
use JSON;
my $url="http://xxx?p=".$phone."&c=$smsmsg";
my $json = JSON->new();
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new('POST', $url);
my $response = $ua->request($req);
my $ret = $json->decode($response->decoded_content());
#复杂点的,PSOT内容为xml格式,请求为https
my $request_xml = create_xml_data($tmp); #构造 xml格式的字符串
my $header = HTTP::Headers->new( Content_Type => 'text/xml; charset=utf8', );
my $http_request = HTTP::Request->new( POST => "https://xxx", $header, $request_xml);
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, SSL_verify_mode => 0x00 });
my $response = $ua->request($http_request);
# PSOT内容为JSON格式
my $header = HTTP::Headers->new( Content_Type => 'application/json; charset=utf8', );
my $param_json_str =$json->encode($post_data); #构造JSON格式的字符串
my $http_request = HTTP::Request->new( POST => "https://xxxxx", $header, $param_json_str );
#特殊POST, https服务器需要客户端上传证书
#以微信现金红包发放API为例,https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
#证书说明:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3
#
my $header = HTTP::Headers->new( Content_Type => 'text/xml; charset=utf8', );
my $http_request = HTTP::Request->new( POST => "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", $header, $request_xml);
my $ua = LWP::UserAgent->new(
ssl_opts => {
verify_hostname => 0,
#SSL_verify_mode => 0x00,
#SSL_ca_file => '/var/wechat/rootca.pem' # 如果客户端也要去验证微信服务器,则需要导入CA证书
SSL_use_cert => 1,
SSL_cert_file => '/var/wechat/apiclient_cert.pem', # 证书文件pem格式
SSL_key_file => '/var/wechat/apiclient_key.pem', # 证书密钥pem格式
SSL_passwd_cb => sub { $config->{mch_id} },# 证书密码
},
);
my $response = $ua->request($http_request);
#特殊POST,服务器要求Content-Type: application/x-www-form-urlencoded格式post
#必须采用下列方式,因为接口要求Content-Type: application/x-www-form-urlencoded
#参考perl网站:https://metacpan.org/pod/HTTP::Request::Common
use HTTP::Request::Common
use LWP::UserAgent;
use JSON;
方式一:
my $response = $ua->request(POST 'https://xxxxx',
[
distributionid => $ID,
distributionkey => $KEY,
request_type => 1,
send_type => 1,
industry => "互联网/电子商务",
sent_way => 3,
merchants => "xxxx",
device_id => "9823413111"
]
);
方式二:
my $params = {
distributionid => $ID,
distributionkey => $KEY,
request_type => 1,
send_type => 1,
industry => "互联网/电子商务",
sent_way => 3,
merchants => "xxxx",
device_id => "9823413111"
};
my $tmp = join( '&',
map { sprintf( '%s=%s', $_, $params->{$_} ) }
sort { $a cmp $b } keys %$params );
my $content = uri_escape_utf8($tmp); # 区别于方式一,这里需要自行做urlencode
my $header = HTTP::Headers->new( Content_Type => 'application/x-www-form-urlencoded; charset=utf8', );
my $http_request = HTTP::Request->new( POST => 'https://xxxxx', $header, $content );
my $ret;
if ($response->message ne "OK" && $response->is_success ne "1") {
$ret->{msg} = "error";
} else {
my $json = JSON->new();
$ret = $json->decode($response->decoded_content());
}
# 使用代理方式发送请求
$ua->timeout(2);
$ua->proxy(['http'], "http://$proxyAddr"); # $proxyAddr 可以是ip加端口例如 "172.1.1.1:128", 相应的也可以用https代理
# 添加http协议头
my @header = ( "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Charset" => "utf-8, iso-8859-1, utf-16, *;q=0.7",
"User_Agent" => "Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; MI 3W Build/MMB29M)",
"X-Requested-With" => "com.android.browser",
#"X-Forward-For" => $ip
);
my $req = HTTP::Request->new( GET => $url);
$req->header(@header);
my $response = $ua->request($req);