views:

418

answers:

2
#!/usr/bin/perl

use WWW::Mechanize;
use Compress::Zlib;

my $mech = WWW::Mechanize->new();

my $username = ""; #fill in username here
my $keyword = "";  #fill in password here

my $mobile = $ARGV[0];
my $text = $ARGV[1];

$deb = 1;

print length($text)."\n" if($deb);

$text = $text."\n\n\n\n\n" if(length($text) < 135);

$mech->get("http://wwwl.way2sms.com/content/index.html");
unless($mech->success())
{
 exit;
}
$dest = $mech->response->content;

print "Fetching...\n" if($deb);

if($mech->response->header("Content-Encoding") eq "gzip")
{
 $dest = Compress::Zlib::memGunzip($dest);
 $mech->update_html($dest);
}

$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;


$mech->update_html($dest);
$mech->form_with_fields(("username","password"));
$mech->field("username",$username);
$mech->field("password",$keyword);

print "Loggin...\n" if($deb);

$mech->submit_form();

$dest= $mech->response->content;

if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

$mech->get("http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0");
$dest= $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

print "Sending ... \n" if($deb);

$mech->form_with_fields(("MobNo","textArea"));
$mech->field("MobNo",$mobile);
$mech->field("textArea",$text);
$mech->submit_form();

if($mech->success())
{
print "Done \n" if($deb);
}
else
{
print "Failed \n" if($deb);
exit;
}

$dest =  $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
  #print $dest if($deb);
}

if($dest =~ m/successfully/sig)
{
  print "Message sent successfully" if($deb);
}

exit;

When run this code halts with an error saying:

There is no form with the requested fields at ./sms.pl line 65
Can't call method "value" on an undefined value at /usr/share/perl5/vendor_perl/WWW/Mechanize.pm line 1348.

+2  A: 

I would guess that there is no form with the fields "MobNo" & "textArea" in http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0 which indeed there aren't since the page at that URL lacks even a <body> tag.

msw
A: 

When I run into these sorts of problems, I print the entire HTML page so I can look at it. The form you are expecting probably isn't there. I suspect that you aren't getting the page that you think you are.

The first page does quite a bit of JavaScript processing to submit the form. Since WWW::Mechanize doesn't handle any of those bits for you, I'm guessing that your first form submission is somehow incomplete or invalid, so the next page you get is some sort of error page. That's a quite common problem with dynamic websites.

You can also compare what Mech does with what a JavaScript-enabled browser does. Use some sort of HTTP sniffing tool to watch the transactions. Is the interactive browser doing something extra that Mech isn't?

brian d foy