Created
July 15, 2012 19:53
-
-
Save babuvignesh/3118368 to your computer and use it in GitHub Desktop.
SMS 160by2 API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
******************************************************************************************************************** | |
* Author : Babu Vignesh | |
* Blog : babuvignesh.posterous.com | |
* Description : A simple free SMS API to send SMS using java and httpclient library with web service of 160by2.com . | |
******************************************************************************************************************** | |
*/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.URLEncoder; | |
import java.util.List; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.CookieStore; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.protocol.ClientContext; | |
import org.apache.http.cookie.Cookie; | |
import org.apache.http.impl.client.BasicCookieStore; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.BasicHttpContext; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.util.EntityUtils; | |
public class BrowserProxy { | |
HttpClient httpclient = new DefaultHttpClient(); | |
CookieStore cookieStore = new BasicCookieStore(); | |
HttpContext localContext = new BasicHttpContext(); | |
HttpGet httpget; | |
HttpResponse response; | |
HttpEntity entity; | |
List<Cookie> cookies; | |
Cookie cookie = null; | |
private String action1; | |
final String sendSMSPage = "http://160by2.com/SendSMS?"; | |
final String sendSMSActionPage = | |
"http://160by2.com/SendSMSAction?hid_exists=yes"; | |
final String loginPage = "http://160by2.com/re-login?"; | |
public BrowserProxy() { | |
} | |
public boolean getLoggedIn(String uname, String pwd) throws IOException, | |
ClientProtocolException { | |
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); | |
URI uri = null; | |
uri = URI.create(loginPage + "username=" + uname + "&password=" + pwd); | |
HttpGet httpget = new HttpGet(uri); | |
response = httpclient.execute(httpget, localContext); | |
entity = response.getEntity(); | |
cookies = cookieStore.getCookies(); | |
Cookie cookie = cookies.get(0); | |
String cookieSet = cookie.getName() + "=" + cookie.getValue(); | |
EntityUtils.consume(entity); | |
fetchAction(cookieSet); | |
if (isLoggedIn()) | |
return true; | |
else | |
return false; | |
} | |
private void fetchAction(String cookieSet) throws IOException { | |
URLConnection conn = new URL(sendSMSPage).openConnection(); | |
conn.setRequestProperty("Cookie", cookieSet); | |
InputStreamReader isr = new InputStreamReader(conn.getInputStream()); | |
BufferedReader br = new BufferedReader(isr); | |
String content; | |
StringBuilder sBuilder = new StringBuilder(); | |
while (true) { | |
content = br.readLine(); | |
if (content == null) | |
break; | |
else if (content.contains("id=\"action1\"")) { | |
sBuilder.append(content); | |
break; | |
} | |
} | |
String action[] = content.split("value=\""); | |
String action1[] = action[1].split("\""); | |
setAction1(action1[0]); | |
} | |
public void sendSms(String to, String msg) throws URISyntaxException, | |
UnsupportedEncodingException, | |
IOException, | |
ClientProtocolException { | |
URI uri = null; | |
msg = URLEncoder.encode(msg, "utf-8"); | |
String params = | |
"&action1=" + getAction1() + "&mobile1=" + to + "&msg1=" + msg; | |
uri = new URI(sendSMSActionPage + params); | |
httpget = new HttpGet(uri); | |
httpclient.execute(httpget, localContext); | |
} | |
public boolean isLoggedIn() { | |
try { | |
if (cookies.size() == 2) | |
return true; | |
else | |
return false; | |
} catch (NullPointerException e) { | |
return false; | |
} | |
} | |
public void setAction1(String action1) { | |
this.action1 = action1; | |
} | |
public String getAction1() { | |
return action1; | |
} | |
public static void main(String[] args) throws IOException, | |
ClientProtocolException { | |
BrowserProxy proxy = new BrowserProxy(); | |
String username = "your 160b2 ten digit mobile number username"; | |
String password = "your password"; | |
String to = "ten digit mobile number"; | |
String msg = "your message as string"; | |
try { | |
proxy.getLoggedIn(username, password); | |
proxy.sendSms(to, msg); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Hi,
Am getting this error-
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
and some exceptions
how can i solve it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now I am getting "Connection to http://160by2.com refused". How to solve this?