HTTP::Request

HTTP::Request

SYNOPSIS ^

 require HTTP::Request;
 $request = HTTP::Request->new(GET => 'http://www.example.com/');
 
 通常使用为:
and usually used like this:

 $ua = LWP::UserAgent->new;
 $response = $ua->request($request);


/*****************************

[root@wx03 ~]# perl a21.pl 
$VAR1 = bless( {
                 '_content' => 'this is test from scan',
                 '_msg' => 'OK',
                 '_protocol' => 'HTTP/1.1',
                 '_rc' => '200',
                 '_request' => bless( {
                                        '_headers' => bless( {
                                                               'user-agent' => 'libwww-perl/6.15'
                                                             }, 'HTTP::Headers' ),
                                        '_content' => '',
                                        '_uri' => bless( do{(my $o = 'http://120.55.118.6:3000/admin/api/menu')}, 'URI::http' ),
                                        '_method' => 'GET',
                                        '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
                                      }, 'HTTP::Request' ),
                 '_headers' => bless( {
                                        'date' => 'Mon, 11 Jul 2016 06:09:29 GMT',
                                        'client-date' => 'Mon, 11 Jul 2016 06:09:29 GMT',
                                        '::std_case' => {
                                                          'client-peer' => 'Client-Peer',
                                                          'client-date' => 'Client-Date',
                                                          'client-response-num' => 'Client-Response-Num'
                                                        },
                                        'content-length' => '22',
                                        'client-response-num' => 1,
                                        'client-peer' => '120.55.118.6:3000',
                                        'content-type' => 'text/html;charset=UTF-8',
                                        'server' => 'Mojolicious (Perl)'
                                      }, 'HTTP::Headers' )
               }, 'HTTP::Response' );
11111111111111111
this is test from scan[root@wx03 ~]# 
[root@wx03 ~]# cat a21.pl 
 use HTTP::Request;
 use LWP::UserAgent;  
  use Data::Dumper;
 $request = HTTP::Request->new(GET => 'http://120.55.118.6:3000/admin/api/menu');
  $ua = LWP::UserAgent->new;
 $response = $ua->request($request);
  print Dumper($response);
 
print "11111111111111111
";
print $response->content;


[root@wx03 ~]# perl a21.pl 
$VAR1 = bless( {
                 '_request' => bless( {
                                        '_uri_canonical' => bless( do{(my $o = 'http://120.55.118.6:3000/admin/api/menu')}, 'URI::http' ),
                                        '_uri' => $VAR1->{'_request'}{'_uri_canonical'},
                                        '_headers' => bless( {
                                                               'user-agent' => 'libwww-perl/6.15'
                                                             }, 'HTTP::Headers' ),
                                        '_method' => 'GET',
                                        '_content' => ''
                                      }, 'HTTP::Request' ),
                 '_rc' => '200',
                 '_content' => 'this is test from scan',
                 '_protocol' => 'HTTP/1.1',
                 '_msg' => 'OK',
                 '_headers' => bless( {
                                        'client-response-num' => 1,
                                        '::std_case' => {
                                                          'client-peer' => 'Client-Peer',
                                                          'client-response-num' => 'Client-Response-Num',
                                                          'client-date' => 'Client-Date'
                                                        },
                                        'client-peer' => '120.55.118.6:3000',
                                        'content-length' => '22',
                                        'content-type' => 'text/html;charset=UTF-8',
                                        'client-date' => 'Mon, 11 Jul 2016 06:09:39 GMT',
                                        'server' => 'Mojolicious (Perl)',
                                        'date' => 'Mon, 11 Jul 2016 06:09:39 GMT'
                                      }, 'HTTP::Headers' )
               }, 'HTTP::Response' );
11111111111111111
this is test from scan[root@wx03 ~]# 
[root@wx03 ~]# 




描述:

HTTP::Request 是一个类封装了HTTP 类型请求, 由一个请求行,一些headers和一个内容body组成。


注意 LWP 库使用HTTP协议请求甚至对于非HTTP协议, 这个类的实例是通常传递到一个LWP::UserAgent object.的request() method
 require HTTP::Request;
 $request = HTTP::Request->new(GET => 'http://www.example.com/');
 
 通常使用为:
and usually used like this:

 $ua = LWP::UserAgent->new;
 $response = $ua->request($request);


HTTP::Request  是一个 HTTP::Message的子类 因为继承了它的方法, 下面额外的方法是提供的:


$r = HTTP::Request->new( $method, $uri )
$r = HTTP::Request->new( $method, $uri, $header )
$r = HTTP::Request->new( $method, $uri, $header, $content )

构造一个新的 HTTP::Request object 描述一个请求在一个对象 $uri 使用方法$method
 $request = HTTP::Request->new(GET => 'http://www.example.com/');
 
 
$method 参数必须是一个字符串,

$uri 蚕食可以是一个字符串,或者一个引用到一个URI 对象。

额外的 $header 参数应该是一个引用到一个 HTTP::Headers object  或者一个 键值对的数组引用。

额外的$content参数必须是一个字符串



$r = HTTP::Request->parse( $str )

这个构造一个新的请求对象通过 解析一个给定的字符串


$r->method
$r->method( $val )


这是用于get/set 方法属性,方法应该是一个短的字符串 像  "GET", "HEAD", "PUT" or "POST".


$r->header( $field )
$r->header( $field => $value )

这是用来get/set header 值 它是继承于HTTP::Headers 通过HTTP::Message


$r->content
$r->content( $bytes )


这是用于get/set 内容,它是继承于HTTP::Message base类。

注意 content应该是一个字符串,字符串在perl可以包含字符以外的字节范围。

sub  wx_init {
               #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-$now&lang=zh_CN&pass_ticket=$pass_ticket";
               my @chatroom_id = ();
               #my $response= $browser->post("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit", [ "r" => "-$now","lang" => "zh_CN","pass_ticket"=>"$pass_ticket"]); #多加了
               $login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-$now&lang=zh_CN&pass_ticket=$pass_ticket";
               my $post = {
                          BaseRequest => {
                          Uin      => $wxuin,
                          Sid      => $wxsid,
                          Skey     => $Skey,
                         DeviceID => $DeviceID,
                                         }
                          };
              use JSON qw(encode_json);
             $json_string = encode_json($post);

             my $req = HTTP::Request->new(
                                 'POST' => $login_url
								 
                                          );
            $req->referer("https://wx.qq.com/?&lang=zh_CN");
            $req->content_type('application/json; charset=UTF-8');   
			#post请求,如果有发送参数,必须要有这句
            $req->content("$json_string");    #发送post的参数
            my $res = $ua->request($req);
            print $res->content();            #获取的是响应正文

		};

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199622.html