Вопрос

I am trying to login to Way2sms through LWP. The source code of the page says that the login form (which, in this case, appears to be the root node of everything within the body) has an action attribute of '', which I read in some other Stackoverflow post, points to the URL of the page containing that form. The name attribute for the mobile and password fields are available from the source code, so I tried this:

use URI;
use HTML::TreeBuilder;
use LWP;
open f, "> way2sms.txt";
$browser = LWP::UserAgent->new(agent=>'Mozilla/4.76 [en] (Windows NT 5.0; U)');
$resp = $browser->post("http://site2.way2sms.com/content/index.html",[username=>$username,password=>$password]);
if ( $resp->is_redirect ) {
    $resp = $browser->get( $resp->header('Location') );
}

print f $resp->content;
close f;

With the correct phone-number and passwords, the submission should return a personalized page, which among other things, contains a message like Welcome, Kaustav Mukherjee. So naturally, I expect the code to print out 5. However, although Logged is printed out denoting success, the 5 isn't printed out, showing that the login failed. How to do it? (Please don't suggest using Mechanize!)

Это было полезно?

Решение

The response that you get is a 302 Moved Temporarily and not a 200 OK. Therefore, you need to fetch the content of the new page and not the content of the redirect response.

#!/usr/bin/env perl

use strict;
use warnings;

use LWP::UserAgent;

my $ua = LWP::UserAgent->new(
    agent =>
      'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
    cookie_jar => {},
);
my $response = $ua->post(
    'http://site2.way2sms.com/Login1.action',
    {
        username => '1234567890',
        password => 'topsecret',
    }
);
if ( $response->is_redirect ) {
    $response = $ua->get( $response->header('Location') );
    print $response->decoded_content;
}

Some comments on your code:

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top