Perl 发送http 请求。
来源:互联网 发布:企业数据管理软件 编辑:程序博客网 时间:2024/05/18 19:19
HTTP GET Perl example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use
LWP::UserAgent;
my
$ua
= LWP::UserAgent->new;
my
$server_endpoint
=
"http://192.168.1.1:8000/service"
;
# set custom HTTP request header fields
my
$req
= HTTP::Request->new(
GET
=>
$server_endpoint
);
$req
->header(
'content-type'
=>
'application/json'
);
$req
->header(
'x-auth-token'
=>
'kfksj48sdfj4jd9d'
);
my
$resp
=
$ua
->request(
$req
);
if
(
$resp
->is_success) {
my
$message
=
$resp
->decoded_content;
print
"Received reply: $message\n"
;
}
else
{
print
"HTTP GET error code: "
,
$resp
->code,
"\n"
;
print
"HTTP GET error message: "
,
$resp
->message,
"\n"
;
}
HTTP POST Perl example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use
LWP::UserAgent;
my
$ua
= LWP::UserAgent->new;
my
$server_endpoint
=
"http://192.168.1.1:8000/service"
;
# set custom HTTP request header fields
my
$req
= HTTP::Request->new(
POST
=>
$server_endpoint
);
$req
->header(
'content-type'
=>
'application/json'
);
$req
->header(
'x-auth-token'
=>
'kfksj48sdfj4jd9d'
);
# add POST data to HTTP request body
my
$post_data
=
'{ "name": "Dan", "address": "NY" }'
;
$req
->content(
$post_data
);
my
$resp
=
$ua
->request(
$req
);
if
(
$resp
->is_success) {
my
$message
=
$resp
->decoded_content;
print
"Received reply: $message\n"
;
}
else
{
print
"HTTP POST error code: "
,
$resp
->code,
"\n"
;
print
"HTTP POST error message: "
,
$resp
->message,
"\n"
;
}
注意:
参数的传递格式有:
$req->header('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8');
#$req->header('content-type' => 'application/json');
#$req->content_type('text/html')
对应的参数格式是不一样的。
0 0
- Perl 发送http 请求。
- 用perl发送http请求
- 用perl发送http请求
- C#发送HTTP请求
- java 发送http请求
- socket 发送http请求
- python发送HTTP请求
- 异步发送HTTP请求
- python发送HTTP请求
- Qt发送HTTP请求
- socket发送http请求
- 发送http请求
- JAVA发送HTTP请求
- python发送HTTP请求
- c++ 发送http请求
- Qt发送http请求
- Qt发送HTTP请求
- 模拟发送http请求
- 1“三水”、“三油”
- 黑马程序员-java面向对象(多态)
- SIM卡信息初始化和联系人读取
- 二进制
- python模拟登录及表单提交
- Perl 发送http 请求。
- 第13周项目5(1-1)-字符串操作
- 二进制 java Class文件解析实例
- java.util.concurrent 多线程框架
- MySQL添加伪序号列
- 工程化的思想
- 【关于codeforces比赛规则介绍】(转载)
- 第13周项目5(1-2)-字符串操作
- C++ 单链表