n w    w w w w

baner

Knowledge is power, and what about sharing the power. CodeGlobe is an open space where we programmers and techies share what we got special with us, The experience we got over years through our profession, the magical touch we made in solving logical hurdles, the new solutions we developed ourselves where others hesitated, alike all these, each and every bit of knowledge we got, let's just share with our pals. All we have to posses is just a mind to speak out and listen others. Registrations are open and hope you won't fail in grabbing this opportunity to become a Codeglober.


 


 

You are here:   Home / API / Way2SMS
large small default
Way2sms
Way2SMS API Print E-mail
Written by Nimish T   
Tuesday, 16 March 2010 18:58

Zion SMS Mania 1.1 beta  our example Application

Download Developed by CodeGlober  Joji

Website URL: xpedition009

 

This article will give you a brief introduction regarding the implementation of way2sms API.We  providing you various flavors of the API in PHP, C# and  Java etc which will helps  you to integrate the free SMS functionlity with in your application using your user credentials in Way2SMS. This program using very simple technique like http client connection to establish a connection with the way2sms website. If you get the thinks better means you can write your own API for many of your favourite application which helps you to integrate that with in your system.

important thing begins from the. This is just like a small type of hacking.

Java Source code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Vector;

public class SMS {
public static void send(String uid, String pwd, String phone, String msg)
throws IOException {
if ((uid == null) || (uid.length() == 0)) {
throw new IllegalArgumentException("User ID should be present.");
}
uid = URLEncoder.encode(uid, "UTF-8");

if ((pwd == null) || (pwd.length() == 0)) {
throw new IllegalArgumentException("Password should be present.");
}
pwd = URLEncoder.encode(pwd, "UTF-8");

if ((phone == null) || (phone.length() == 0)) {
throw new IllegalArgumentException(
"At least one phone number should be present.");
}
if ((msg == null) || (msg.length() == 0)) {
throw new IllegalArgumentException("SMS message should be present.");
}
msg = URLEncoder.encode(msg, "UTF-8");

Vector numbers = new Vector();

if (phone.indexOf(59) >= 0) {
String[] pharr = phone.split(";");
for (String t : pharr)
try {
numbers.add(Long.valueOf(t));
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(
"Give proper phone numbers.");
}
} else {
try {
numbers.add(Long.valueOf(phone));
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Give proper phone numbers.");
}
}

if (numbers.size() == 0) {
throw new IllegalArgumentException(
"At least one proper phone number should be present to send SMS.");
}
String temp = "";
String content = "username=" + uid + "&password=" + pwd;
URL u = new URL("http://wwwa.way2sms.com/auth.cl");
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String
.valueOf(content.length()));
uc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Referer", "http://wwwg.way2sms.com//entry.jsp");
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(uc
.getOutputStream()), true);
pw.print(content);
pw.flush();
pw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
String cookie = uc.getHeaderField("Set-Cookie");

u = null;
uc = null;
for (Iterator localIterator = numbers.iterator(); localIterator
.hasNext();) {
long num = ((Long) localIterator.next()).longValue();

content = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo="
+ num
+ "&textArea="
+ msg;
u = new URL("http://wwwa.way2sms.com/FirstServletsms?custid=");
uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String.valueOf(content
.getBytes().length));
uc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false);
pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()),
true);
pw.print(content);
pw.flush();
pw.close();
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((temp = br.readLine()) != null)
;
br.close();
u = null;
uc = null;
}

u = new URL("http://wwwa.way2sms.com/jsp/logout.jsp");
uc = (HttpURLConnection) u.openConnection();
uc
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("GET");
uc.setInstanceFollowRedirects(false);
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((temp = br.readLine()) != null)
;
br.close();
u = null;
uc = null;
}
}

 

 

 

Explanation JAVA example

String content = "username=" + uid + "&password=" + pwd;
First of all you have to submit username and password to the following url
http://wwwa.way2sms.com/auth.cl for that you have to make an HttpUrlConnection with the website link
This you can achieve by doing this
URL u = new URL("http://wwwa.way2sms.com/auth.cl");
HttpURLConnection uc = (HttpURLConnection) u.openConnection();


After that you have to set attributes for the HttpURLConnection you made. Most of the attributes you can identify by the name itself. These all are http header value that is set by the browser while we sibmit the login page of  the way2sms which we manually setting and send to the server using the print method of PrintWriter object.

uc.setDoOutput(true);
uc.setRequestProperty("User-

Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122

Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String.valueOf(content.length()));
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Referer", "http://wwwg.way2sms.com//entry.jsp");
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()), true);
pw.print(content);
pw.flush();
pw.close();

The below statements will parse the whole pages after successful login.

BufferedReader br = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
while ((temp = br.readLine()) != null) {
}

After this you have to fetch the cookie from the header and store it to somewhere this will be helpful for further contact with the server.
String cookie = uc.getHeaderField("Set-Cookie");
Here your login process is complted. Next step is to send sms to specified mobile number.

These are the datas that we have to use for send sms.
content = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo="+ num+

"&textArea="+msg;

below given is the url for send sms.This servlet will send message to specified phone number.

u = new URL("http://wwwa.way2sms.com/FirstServletsms?custid=");
uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122

Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");

you have to pass previously stored cookie with this data.

uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false);
pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()),true);
pw.print(content);
pw.flush();
pw.close();
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((temp = br.readLine()) != null)
;


After this you have to do the logout process


u = new URL("http://wwwa.way2sms.com/jsp/logout.jsp");
uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122

Firefox/3.0.5");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("GET");
uc.setInstanceFollowRedirects(false);
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((temp = br.readLine()) != null)
;
Way2SMS C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace SMSAPI
{
class SmsSender
{
void send(string uid, string pwd, string no, string msg)
{
String content = "username="+uid+"&password="+pwd;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/auth.cl");
request.KeepAlive = false;
byte[] byteArray = Encoding.UTF8.GetBytes(content);
CookieContainer cookies = new CookieContainer();
request.CookieContainer = cookies;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Referer = "http://wwwg.way2sms.com//entry.jsp";
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie cook in response.Cookies)
{
cookies.Add(cook);
}
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string serverData = reader.ReadToEnd();
reader.Close();
content = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo="+no+"&textArea="+msg;
request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/FirstServletsms?custid=");
byteArray = Encoding.UTF8.GetBytes(content);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.CookieContainer = cookies;
request.Method = "POST";
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream);
serverData = reader.ReadToEnd();
reader.Close();
request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/jsp/logout.jsp");
byteArray = Encoding.UTF8.GetBytes(content);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.CookieContainer = cookies;
request.Method = "POST";
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream);
serverData = reader.ReadToEnd();
reader.Close();
}
catch (ArgumentException e)
{
Console.WriteLine("arg exception");
Console.Read();

}
catch (WebException e)
{
Console.WriteLine("web exception");
Console.Read();
}
catch (Exception e)
{
Console.WriteLine("exception");
Console.Read();
}
}

static void Main(string[] args)
{
SmsSender sms = new SmsSender();
sms.send("username", "password", "phno_recipient", "message");
}

}
}
Way2SMS PHP code
$post_data = "username=username&password=pwd";
//$header_array[]="User-Agent:
$url = "http://wwwa.way2sms.com/auth.cl";
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch,CURLOPT_REFERER,"http://wwwg.way2sms.com//entry.jsp");
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
//$ch = curl_init($url);
//echo $content;
//value of custfrom u have to change according to u'r
$post_data = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo=9986966496&textArea=test sms";
$url = "http://wwwa.way2sms.com/FirstServletsms?custid=";
curl_setopt( $ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 0 );
$content = curl_exec( $ch );
$url = "http://wwwa.way2sms.com/jsp/logout.jsp";
curl_setopt( $ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 0 );
$content = curl_exec( $ch );
//echo $content;
?>


Last Updated on Thursday, 18 March 2010 13:00