Perl Http处理模块-HTTP::Client
HTTP::Client - Class for making HTTP requestsHTTP::Client is a class for creating clients, that does not require LWP. It is aimed at speed. It can send HTTP Request headers, get HTTP response headers and get documents.
HTTP::Client是一个较为简单的http客户,不需要依赖LWP(但对LWP::Online有依赖)。它的目的是快速处理,可以用它发送HTTP请求头,处理HTTP响应头和取得文档。
可用的方法:
new
This is the constructor for HTTP::Client. It can be called like this:
my $client = HTTP::Client->new;
my $client = new HTTP::Client ();
或指定一个useragent作为其参数:
my $client = HTTP::Client->new("Bot/1.0");
$client->get
get gets a page on the web. It can only do http pages for now. The URI (URL) to get is the only argument it takes. It returns the body of the site if successful (The HTTP status code is "200 OK", or the other HTTP status code if it is not equal to that. For example:
my $site = $client->get("http://www.cpan.org");
print $site . "\n";
将会输出页面的html代码。
$client->response_headers
response_headers returns an array of all the response headers sent by the server. Currently, to loop through the array.
以数组的形式返回响应头。
$client->agent
agent sets the current User-Agent header, thus renaming the current client. If the agent was specified in the constructor, even using this method will not override the one specified in the constructor. So, you can only override your useragent with multiple agent()s, not with the constructor and then agent()s.
指定User-Agent头,自定义参数。
$client->from
from sets the From header, which should be an email address of the person/machine that is sending the request. If the from was specified in the constructor, just like the agent method, using this method will not override the setting in the constructor. Therefore, you can only override from headers by using multiple from() methods, not by using the constructor and then from methods.
$client->status_message
status_message returns the HTTP status message sent by the server. It returns it in the full form, e.g. "200 OK" or "404 Not Found".
返回http的服务器响应的状态消息。
$client->server
returns the value of the Server header sent by the server.
$client->content_type
returns the Content-Type header sent by the server.
$client->last_modified
returns the Last-Modified header sent by the server.
$client->protocol
returns the protocol sent by the server. (Usually something like "HTTP/1.1".
$client->content_encoding
returns the Content-Encoding header sent by the server.
$client->content_length
returns the Content-Length header sent by the server.
$client->warning
returns the Warning header sent be the server.
$client->title
returns the Title header sent by the server.
Note: This is no longer part of the HTTP specification.
$client->date
returns the Date header returned by the server.
$client->host
returns the Host header returned by the server.
个人感觉不及HTTP::Lite好用,更不用说HTTP::Tiny了,使用时有些api都没有返回响应,慎用。
最新版本:1.5
项目主页:https://metacpan.org/release/HTTP-Client