如何在Perl中的URL上发送请求

时间:2013-08-28 11:36:21

标签: perl

我想在特定URL上发送XML请求并从那里获取resposne。如何通过创建A Module来实现Perl。我是Perl的新手请帮帮我。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

package TEST::Http;
use strict;
use warnings;
use HTTP::Request;
use LWP::UserAgent;
use HTTP::Headers;


sub new {
    my $class = shift;
    my $this = {};
    bless $this, $class;
    return $this;
}

sub send_receive {
    my $this    = shift;
    my $args    = shift;
    $this->{pua}    = LWP::UserAgent->new();
    $this->{header} = HTTP::Headers->new;
    $this->{header}->header("Content-Type" => "text/xml", "SOAPAction" =>"");

    my ($request, $response);
    my $Response = {};
    eval {
        local $SIG{ALRM} = sub {die "Timed out"};
        alarm 90;
        $request = HTTP::Request->new( "POST", $args->{URL} , $args->{xml_request});
        $response = $this->{pua}->simple_request($request);
        alarm 0;
    };
    return $response->content;
}

sub DESTROY {
    my $this = shift || return;
}

1;