Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SMS 160by2 API
/*
********************************************************************************************************************
* 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());
}
}
}
@SourabhSadhu

This comment has been minimized.

Copy link

@SourabhSadhu SourabhSadhu commented Sep 22, 2016

Hi,
Am getting this error-
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.AbstractHttpClient.(AbstractHttpClient.java:182)
at org.apache.http.impl.client.DefaultHttpClient.(DefaultHttpClient.java:150)
at BrowserProxy.(BrowserProxy.java:37)
at BrowserProxy.main(BrowserProxy.java:134)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
I have included apache-httpcomponents-httpcore and apache-httpcomponents-httpclient jars.
Please let me know how to solve it.

@SourabhSadhu

This comment has been minimized.

Copy link

@SourabhSadhu SourabhSadhu commented Sep 22, 2016

Now I am getting "Connection to http://160by2.com refused". How to solve this?

@anshuljoshi143

This comment has been minimized.

Copy link

@anshuljoshi143 anshuljoshi143 commented Mar 7, 2017

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
You can’t perform that action at this time.