perl http get和post

来源:互联网 发布:局域网现场直播软件 编辑:程序博客网 时间:2024/05/18 11:14
 

转载地址:http://mutudumutu.spaces.live.com/blog/cns!495E756F3B5E8D74!135.entry

#! /usr/bin/perl -w
#www_get.pl
#这是学习perl编程的代码
#HTTP协议的GET操作
#author: mutudumutu@hotmail.com
#date:2006-03-03
#

use strict;
use LWP::UserAgent;
my $method = shift || 'GET';
my $server_name = shift || 'localhos';
my $port_number = shift || 80;
my $page = shift || 'index.html';

my $url = 'http://'. $server_name . ':'. $port_number.'/' . $page;
my $request = new HTTP::Request $method => $url;

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

print $response->as_string;
__END__

=======================================
下面这个可以提交数据的
#! /usr/bin/perl -w
#www_post.pl
#这是学习perl编程的代码
#HTTP协议的POST操作
#author: mutudumutu@hotmail.com
#date:2006-03-03
#
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;

#得到要提交的参数
my $server_name = shift || 'localhost';
my $port_number = shift || '8443';
my $page = shift || 'formtest.do';

my $url = 'https://'. $server_name . ':'. $port_number.'/' . $page;
my $useragent = new LWP::UserAgent;
my $response = $useragent->request(POST $url ,[para1=>'value1', para2=>'value2']);

print $response->as_string;
__END__
虽然这两个代码很弱,但是对Http的Get和Post都有了,与Http的交互可以从这两个代码扩展。

原创粉丝点击