How to set up REFERER and other HTTP header parameters?
How to get specified part of file (first MAXSIZE bytes)?
How to get and set up cookies?
How to specify proxy servers?
How to check for redirect?
How to create parameters for POST method?
How to upload file?
How to access secure site (https://)
How to get information about remote file?
How to do "redirect" that works with both GET and POST?
(Is it the same as "forward" does in Java?)
====================================================================
1. Как получить текстовый файл, используя http или ftp?
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://www.yahoo.com/';
my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
print header, $res->is_success ? $res->content : $res->status_line;
1.4. How to set up REFERER and other HTTP header parameters?
use LWP::UserAgent;
use HTTP::Headers;
use CGI qw(header -no_debug);
my $URL = 'http://localhost/cgi-bin/hello.cgi';
my $res = LWP::UserAgent->new->request(
new HTTP::Request(
GET => $URL,
new HTTP::Headers referer => 'http://www.yahoo.com'),
);
print header, $res->is_success ? $res->content : $res->status_line;
1.5. How to get specified part of file (first MAXSIZE bytes)?
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://www.yahoo.com/';
my $MAXSIZE = 1024;
print header;
my $res = LWP::UserAgent->new->request(
new HTTP::Request(GET => $URL), \&callback, $MAXSIZE);
sub callback { my($data, $response, $protocol) = @_; print $data; die }
use LWP::UserAgent;
use CGI qw(header -no_debug);
use HTTP::Cookies;
my $URL = 'http://mail.yahoo.com/';
my $ua = new LWP::UserAgent;
my $res = $ua->request(new HTTP::Request GET => $URL);
my $cookie_jar = new HTTP::Cookies;
$cookie_jar->extract_cookies($res);
print header;
if ($res->is_success) {
my $req = new HTTP::Request GET => $URL;
$cookie_jar->add_cookie_header($req);
$res = $ua->request($req);
print $res->is_success ? $res->as_string : $res->status_line;
} else {
print $res->status_line;
}
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://www.yahoo.com/';
my $ua = new LWP::UserAgent;
$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
$ua->proxy('gopher', 'http://proxy.sn.no:8001/');
my $res = $ua->request(new HTTP::Request GET => $URL);
print header, $res->is_success ? $res->content : $res->status_line;
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://www.yahoo.com/';
my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
print header;
print $res->request->url if $res->previous->is_redirect;
use URI::URL;
use HTTP::Request;
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://yahoo.com/?login=mylogin&password=mypassword';
my $uri = new URI $URL;
my $method = 'POST';
my $request;
if (uc($method) eq 'POST') {
my $query = $uri->query;
(my $url = $uri->as_string) =~ s/\?$query$//;
$request = new HTTP::Request ($method, $url);
$request->header('Content-Type' => 'application/x-www-form-urlencoded');
$request->content($query);
} else {
$request = new HTTP::Request ($method, $uri->as_string);
};
# add Host field as required in HTTP/1.1
$request->header(Host => $uri->host_port) if $uri->scheme ne 'file';
my $res = LWP::UserAgent->new->request($request);
print header, $res->is_success ? $res->content : $res->status_line;
use HTTP::Request::Common;
use LWP::UserAgent;
use CGI qw(header -no_debug);
my $URL = 'http://localhost/cgi-bin/survey.cgi';
my $req = POST $URL,
Content_Type => 'form-data',
Content =>
name => 'Paul Kulchenko',
email => '[email protected]',
surveyfile => ['./survey.dat'], # this file will be uploaded
];
my $res = LWP::UserAgent->new->request($req);
print header, $res->is_success ? $res->content : $res->status_line;