3

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

#!/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 的新手,所以我在这里抓住了稻草。它可能仍然在客户端,但我也想排除我的任何问题。

提前感谢你们可以提供的任何帮助 - 我被谷歌搜索了。

4

3 回答 3

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集成。

于 2013-05-30T22:46:27.547 回答
1

使用 $response->decoded_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;
}
于 2015-04-06T21:02:11.030 回答
0

检查 $response->as_string 的值

它会向您显示带有标头的完整 http 响应

于 2013-05-30T21:51:38.530 回答