大家好,
我第一次用lwp遇到了困难,请大家给点建议。
想用lwp实现下面的情景(在浏览器下手动跑没有问题):浏览器地址栏输入一个URL(https),然后用户被要求输入用户名和密码,输入正确后,弹出了保存文件的对话框,保存文件到本地。
我的做法:我先连同用户名,密码,发送URL,得到的回复如下:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://xxx">here</a>.</p>
</body></html>
我猜这是让我到 href指向的链接去拿文件吧?然后我用TokenParser把这个新的URL解析出来,连同我原来的用户名密码发出去,然后print reseponse->content() 为空!
不知道是哪里不对,这种问题怎么debug呢?
# cat -n lwp.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use LWP;
5 use HTML::TokeParser;
6
7 my $user = 'xxxxxx';
8 my $passwd = 'xxxxxx';
9
10 my $browser = LWP::UserAgent->new();
11
12 my $url = 'https://......';
13 my $response =
14 $browser->post( $url, [ 'username' =>, $user, 'password' => $passwd ], );
15 my $content = $response->content();
16
17 my $stream = HTML::TokeParser->new( \$content );
18 my $relink;
19 while ( my $token = $stream->get_token() ) {
20 if ( $token->[0] eq 'S' ) {
21 my ( $tagname, $attr ) = @$token[ 1, 2 ];
22 print "tag:", $tagname, "\n";
23 if ( $tagname eq 'a' ) {
24 $relink = $attr->{'href'};
25 }
26 }
27 }
28
29 print "relink", $relink, "\n";
30
31 $response = $browser->post(
32 $relink,
33 [
34 username => $user,
35 password => $passwd,
36 ]
37 );
38 print "response begin\n";
39 print $response->content(); # ..
40 print "response end \n";
41
没有评论:
发表评论