使用LWP张贴到表单:: UserAgent没有响应(主要是)

时间:2013-05-30 15:46:21

标签: perl lwp-useragent

这是我的困境:我正在尝试使用LWP :: UserAgent填写Web表单并从该表单返回结果。以下是我的代码示例:

#!/usr/bin/perl -w

use strict;
use LWP;
use HTTP::Request::Common;
use LWP::Debug qw(+);

my $ua = LWP::UserAgent->new(protocols_allowed=>["https"]);

my $req = POST 'https://their.securesite.com/index.php',
[ 'firstName'                   => 'Me',
  'lastName'                    => 'Testing',
  'addressLine1'                => '123 Main Street',
  'addressLine2'                => '',
  'city'                        => 'Anyplace',
  'state'                       => 'MN',
  'zipCode'                     => '55555',
  'card'                        => 'visa',
  'cardNumber'                  => '41111111111111111',
  'ccv2'                        => '123',
  'exp_month'                   => '07',
  'exp_year'                    => '2015',
  'shared_key'                  => 'hellos',
];

my $response = $ua->request($req);

print $response->is_success() . "\n";
print $response->status_line . "\n";
print $response->content . "\n";

当我运行它时,我得到200 OK和“1”表示成功,但不是表单中的响应页面。只是结束标签:

</body>
</html>

这可能是因为表单页面和响应页面都有相同的URL吗?我是LWP的新手,所以我在这里抓住稻草。它可能仍然在客户端,但我也想排除我的任何问题。

提前感谢你们给予的任何帮助 - 我用Google搜索了。

3 个答案:

答案 0 :(得分:1)

如果您可以使用Mojo::UserAgentMojolicious工具套件的一部分),代码将如下所示。请注意,您可能需要IO :: Socket :: SSL才能使用HTTPS。

#!/usr/bin/env perl

use strict;
use warnings;

use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my $tx = $ua->post('https://their.securesite.com/index.php', form => 
{ 'firstName'                   => 'Me',
  'lastName'                    => 'Testing',
  'addressLine1'                => '123 Main Street',
  'addressLine2'                => '',
  'city'                        => 'Anyplace',
  'state'                       => 'MN',
  'zipCode'                     => '55555',
  'card'                        => 'visa',
  'cardNumber'                  => '41111111111111111',
  'ccv2'                        => '123',
  'exp_month'                   => '07',
  'exp_year'                    => '2015',
  'shared_key'                  => 'hellos',
});

if ( $tx->success ) {
  print $tx->res->body;
  # or work with the resulting DOM
  # my $dom = $tx->res->dom;
} else {
  my ($err, $code) = $tx->error;
  print $code ? "$code response: $err\n" : "Connection error: $err\n";
}

界面略有不同,但它有很多不错的功能,包括用于解析响应HTML的Mojo::DOM集成。

答案 1 :(得分:1)

使用$ response-&gt; decoding_content来获取没有标题的内容。有关详细信息,请参阅HTTP::Message

#!/usr/bin/perl -w

use strict;

use URI;
use LWP::UserAgent;
use HTTP::Request;

my $url = URI->new('https://their.securesite.com/index.php');
my $ua = LWP::UserAgent->new();

my $request = HTTP::Request->new(
    'POST',
    $url,
    HTTP::Headers->new(
        'User-Agent'  =>  "perl ua/ v0.001",
        'Accept'    =>  "text/xml, multipart/*, application/soap"
    ),
    [ 'firstName'                   => 'Me',
      'lastName'                    => 'Testing',
      'addressLine1'                => '123 Main Street',
      'addressLine2'                => '',
      'city'                        => 'Anyplace',
      'state'                       => 'MN',
      'zipCode'                     => '55555',
      'card'                        => 'visa',
      'cardNumber'                  => '41111111111111111',
      'ccv2'                        => '123',
      'exp_month'                   => '07',
      'exp_year'                    => '2015',
      'shared_key'                  => 'hellos',
     ]  
) or die "Error initiating Request: $@\n";

my $response = $ua->request( $request );

if ($response->is_success) {
     print $response->decoded_content, "\n";
} else {
    die $response->status_line;
}

答案 2 :(得分:0)

检查$ response-&gt; as_string

的值

它会显示带标题的完整http响应