#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    62
    Rep Power
    3

    Automating sms sending by through Way2sms in Perl


    I am trying to send sms through Way2sms using Perl LWP. The login part is being successful, after which I save the cookies to a local file. The welcome page after being logged in shows a Send SMS link, clicking on which one is redirected to another page with two inputs for mobile number and sms text and a button for submitting and sending the sms. Firebug reveals the page structure as shown in the figure. From the Iframe url and the form's action attribute, I constructed the form action's absolute URL and submit the form accordingly, with the cookie stored in the file. However, the sms isn't sent. What I am doing wrong here? The code is as follows. (The name attributes for the two text inputs are correct, taken by observing the source code in Firebug, although that's not included in the image)

    Code:
    use LWP::UserAgent;
    use HTTP::Cookies;
    my $cookie_jar = HTTP::Cookies->new(
    file => "cookies.txt",
    autosave => 1,
    );
    
    my $ua = LWP::UserAgent->new(
    agent =>
      'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
    cookie_jar => $cookie_jar,
    );
    my $response = $ua->post(
    'http://site2.way2sms.com/contentt/bar/Login1.action',
    {
    username => $user,
    password => $pass,
    }
    );
    my $ocookie_jar = HTTP::Cookies->new(
    file => "cookies.txt",
    );
    if ( $response->is_redirect ) {
    $response = $ua->get( $response->header('Location') );
    print 5 if $response->decoded_content =~ /Kaustav Mukherjee/i; #prints 5 if the resultant page contains my name, showing that the login is successful
    }
    my $smspage = LWP::UserAgent->new(agent =>
      'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
    cookie_jar => $ocookie_jar);
    	
    my $smsresp = $smspage->post("http://site5.way2sms.com/jsp/quicksms.action",[MobNo=>$mob,textArea=>'Hello World']);
    Attached Images
    Last edited by Cupidvogel; August 26th, 2012 at 10:38 AM.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,261
    Rep Power
    1810
    The point of accepting cookies is that your browser will store information about a site, and be able to give that information back to a server when you make another request so the server can recognize the identity you provided earlier.

    Imagine opening Internet Explorer and logging in to a website, and then right after that opening Firefox and trying to visit that same website. The server would need you to log in again. It has no idea you are the same person who logged in a moment before.

    That's what you've done in the script. You created a cookie jar and put it in your browser (UserAgent), then created another cookie jar after posting, and then created a new browser to attempt to reach a restricted page.

    Also, check the first URL. Might not be valid.

    Comments on this post

    • Cupidvogel agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    62
    Rep Power
    3
    No no, the first URL may look invalid, but it is valid. I was testing it, whatever I insert between site2.way2sms.com and Login1.action, the page is always redirected. I noted what you said and revised the code. Still the sms is not being sent!

    Code:
    use LWP::UserAgent;
    use HTTP::Cookies;
    my $cookie_jar = HTTP::Cookies->new(
    file => "cookies.txt",
    autosave => 1,
    );
    
    my $ua = LWP::UserAgent->new(
        agent =>
          'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
        cookie_jar => $cookie_jar,
    );
    my $response = $ua->post(
        'http://site2.way2sms.com/contentt/bar/Login1.action',
        {
            username => $user,
            password => $pass,
        }
    );
    
    if ( $response->is_redirect ) {
        $response = $ua->get( $response->header('Location') );
        print 5 if $response->decoded_content =~ /Kaustav Mukherjee/i; #prints it, showing that the login is successful
    }   
    my $smsresp = $ua->post("http://site5.way2sms.com/jsp/quicksms.action",[MobNo=>$mob,textArea=>'Hello World']);
    Last edited by Cupidvogel; August 26th, 2012 at 09:09 AM.
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,261
    Rep Power
    1810
    I don't have an account there, so I can't test any of it. The reason I asked about the URL is the two T's at the end of the word 'contentt'

    Code:
    http://site2.way2sms.com/contentt/bar/Login1.action
    I wasn't able to get to that URL with a browser, so it didn't seem valid, but if you said you logged in then fine.

    But you will need to examine that second form very carefully. It may need more than just the data you want to send. Often times there are hidden fields embedded that will need to be submitted back.

    Check the response object and see what went wrong:

    Code:
    my $smsresp = $ua->post("http://site5.way2sms.com/jsp/quicksms.action",[MobNo=>$mob,textArea=>'Hello World']);
    if ($smsresp->is_success) {
         print $smsresp->content;  # you can print this to a file if you want to check the page in a text editor
     } else {
         die $smsresp->status_line;
     }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    62
    Rep Power
    3
    I just did, and got a redirected status line. So I used the redirection method which I used with the first login method here also. Now I am getting a status 200 OK message, but still no sms. Is there any way of sending you a message? Then I can send you the login details, you can check with that.

IMN logo majestic logo threadwatch logo seochat tools logo