Perl Http处理模块-HTTP::Lite
HTTP::Lite - Lightweight HTTP implementation这是一个小型且简单实用的的http处理库,与HTTP::Tiny模块类似,但功能要弱一些。能适合于绝大多数的http请求处理情景。它的设计初衷为在一定的情况下取代LWP::UserAgent系列的这类http处理库来完成任务,它自身的代码量少、依赖也少、运行速度快等特点。
HTTP::Lite is a stand-alone lightweight HTTP/1.1 implementation for perl. It is not intended as a replacement for the fully-featured LWP module. Instead, it is intended for use in situations where it is desirable to install the minimal number of modules to achieve HTTP support, or where LWP is not a good candidate due to CPU overhead, such as slower processors. HTTP::Lite is also significantly faster than LWP.
HTTP::Lite is ideal for CGI (or mod_perl) programs or for bundling for redistribution with larger packages where only HTTP GET and POST functionality are necessary.
从0.2.1开始,http方法可从post或get中任选,支持http/1.1的主机头,这对基于名称的虚拟主机的支持更多友好,同时也开始支持代理了。
从2.0.0开始,它支持请求回调函数,这们对于下载大文件时显得更加有效,可使用的内存更少。
如果要支持如ftp或https这样的协议时,就用使用如libwwwperl (LWP)此类的多功能http模块,当然它们要复杂些。
示例:
use v5.12;
use HTTP::Lite;
use Data::Dumper;
my $http = HTTP::Lite->new;
my $req = $http->request("http://www.freeoa.net/") or die "Unable to get document: $!";
#print $http->body();
say $http->status."\t".$http->status_message;
say $http->protocol;
my @hd=$http->headers_array;
say Dumper(\@hd);
say $http->headers_string;
my @headers = $http->headers_array();
foreach my $header (@headers){
say "$header";
}
可用的方法:
new
request ( $url, $data_callback, $cbargs )
Initiates a request to the specified URL.
Returns undef if an I/O error is encountered, otherwise the HTTP status code will be returned. 200 series status codes represent success, 300 represent temporary errors, 400 represent permanent errors, and 500 represent server errors.
set_callback ( $functionref, $dataref )
At various stages of the request, callbacks may be used to modify the behaviour or to monitor the status of the request. These work like the $data_callback parameter to request(), but are more versatile. Using set_callback disables $data_callback in request()
The callbacks are called as: callback ( $self, $phase, $dataref, $cbargs )
prepare_post ( $hashref )
Takes a reference to a hashed array of post form variables to upload. Create the HTTP body and sets the method to POST.
http11_mode ( 0 | 1 )
Turns on or off HTTP/1.1 support. This is off by default due to broken HTTP/1.1 servers. Use 1 to enable HTTP/1.1 support.
add_req_header ( $header, $value )
get_req_header ( $header )
delete_req_header ( $header )
Add, Delete, or get a HTTP header(s) for the request. These functions allow you to override any header. Presently, Host, User-Agent, Content-Type, Accept, and Connection are pre-defined by the HTTP::Lite module. You may not override Host, Connection, or Accept.
body
Returns the body of the document returned by the remote server.
headers_array
Returns an array of the HTTP headers returned by the remote server.
headers_string
Returns a string representation of the HTTP headers returned by the remote server.
get_header ( $header )
Returns an array of values for the requested header.
protocol
Returns the HTTP protocol identifier, as reported by the remote server. This will generally be either HTTP/1.0 or HTTP/1.1.
proxy ( $proxy_server )
The URL or hostname of the proxy to use for the next request.
status
Returns the HTTP status code returned by the server. This is also reported as the return value of request().
status_message
Returns the textual description of the status code as returned by the server. The status string is not required to adhere to any particular format, although most HTTP servers use a standard set of descriptions.
reset
You must call this prior to re-using an HTTP::Lite handle, otherwise the results are undefined.
local_addr ( $ip )
Explicity select the local IP address. 0.0.0.0 (default) lets the system choose.
local_port ( $port )
Explicity select the local port. 0 (default and recommended) lets the system choose.
method ( $method )
Explicity set the method. Using prepare_post or reset overrides this setting. Usual choices are GET, POST, PUT, HEAD
不支持
- FTP
- HTTPS (SSL)
- Authenitcation/Authorizaton/Proxy-Authorization are not directly supported, and require MIME::Base64.
- Redirects (Location) are not automatically followed
- multipart/form-data POSTs are not directly supported (necessary for File uploads).
最新版本:2.4
项目主页:https://metacpan.org/release/HTTP-Lite