Java Code Examples

Get Started With EZ Texting's HTTP SMS API Using Our Perl Code Samples

Sending SMS Messages - Code Examples:

Send A Single Message

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class ex1 {

    public static void main(String[] args) throws Exception {

        HttpClient httpclient = new DefaultHttpClient();

        List formparams = new ArrayList();
        formparams.add(new BasicNameValuePair("user", "username"));
        formparams.add(new BasicNameValuePair("pass", "userpassword"));
        formparams.add(new BasicNameValuePair("phonenumber", "2125551234"));
        formparams.add(new BasicNameValuePair("subject", "test"));
        formparams.add(new BasicNameValuePair("message", "test message"));
        formparams.add(new BasicNameValuePair("express", "1"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        HttpPost httppost = new HttpPost("https://app.eztexting.com/api/sending/");
        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity2 = response.getEntity();


        if (entity2 != null) {
            System.out.println(EntityUtils.toString(entity2));
        }
        if (entity2 != null) {
            entity2.consumeContent();
        }


        // When HttpClient instance is no longer needed, 
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();        

    }
}

Send Multiple Messages

When sending the same message to multiple recipients we recommend our REST API.

import java.io.*;
import java.net.*;

//see http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html for how keep-alive is working.
public class ex2 {

    public static void main(String[] args) throws Exception {

        String[][] messages = {
           {"2125551234", "test", "test message"},
           {"2127846501", "test2", "test message2"},
           {"2127846502", "test3", "test message3"}
        };

        String data = "user=username&pass=userpassword&express=1";

        URL url = new URL("https://app.eztexting.com/api/sending/");
        for (int i = 0; i < messages.length; i++)
        {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data+"&phonenumber="+messages[i][0]+"&subject="+messages[i][1]+"&message="+messages[i][2]);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String s;
            while((s = rd.readLine()) != null) System.out.println(s);
     
            wr.close();
            rd.close();
        }
    }
}

Receiving Keyword Submissions - Code Examples:

<%
String message = request.getParameter("Message");
String sender = request.getParameter("PhoneNumber");

if (sender != null) {

    //Save incoming messages
    java.io.FileWriter f = new java.io.FileWriter("receivelog.txt", true);
    f.write(sender+"\n");
    f.close();

    //Return a response SMS message
    String responsetext = "Thank you for the message!";
    out.println("{SMS:TEXT}{}{}{"+sender+"}{"+responsetext+"}");
}
else
{
%>The Java script is waiting for messages<%}%>


Check Credit Balance - Code Examples:

import java.io.*;
import java.net.*;


public class ex4 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword";

        URL url = new URL("https://app.eztexting.com/api/credits/check/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Check Keyword Availability - Code Examples:

import java.io.*;
import java.net.*;


public class ex5 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&keyword=userkeyword";

        URL url = new URL("https://app.eztexting.com/api/keyword/check/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Buy More Credits - Code Examples:

import java.io.*;
import java.net.*;


public class ex6 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&credits=5&firstname=firstname&lastname=lastname&address=address&city=newyork&state=ny&zip=08902&country=usa&type=visa&ccnumber=rIhLJUiXl8M0JIcrelxH9A&cccode=111&expm=11&expy=12";

        URL url = new URL("https://app.eztexting.com/api/credits/buy/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Rent a Keyword: - Code Examples

import java.io.*;
import java.net.*;


public class ex7 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&keyword=keywordtorent&firstname=firstname&lastname=lastname&address=address&city=new york&state=ny&zip=08902&country=usa&type=visa&ccnumber=rIhLJUiXl8M0JIcrelxH9A&expm=11&expy=2012";

        URL url = new URL("https://app.eztexting.com/api/keyword/rent/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Configure a Keyword - Code Examples:

import java.io.*;
import java.net.*;


public class ex8 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&keyword=keywordtoconfigure&group=test&autoreply=autoreply&url=http://test.com/test";

        URL url = new URL("https://app.eztexting.com/api/keyword/setup/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Voice Broadcast - Code Examples:

 

(Single Number)

import java.io.*;
import java.net.*;


public class ex9 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&phonenumbers=2125551234&soundsource=http://mydomain.com/sounds/voice.wav&callerid=2127847840";

        URL url = new URL("https://app.eztexting.com/api/voicemessages/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

 

(An Array Of Numbers)

import java.io.*;
import java.net.*;


public class ex10 {

    public static void main(String[] args) throws Exception {


        String data = "user=username&pass=userpassword&phonenumbers[]=phone1&phonenumbers[]=phone2&phonenumbers[]=phone3&soundfile=1236019718.wav&callerid=2127847840";

        URL url = new URL("https://app.eztexting.com/api/voicemessages/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(rd.readLine());
 
        wr.close();
        rd.close();
    }
}

Receiving Inbox Message Replies - Code Examples:

<%
String message = request.getParameter("message");
String sender = request.getParameter("from");

if (sender != null) {

    //Save incoming messages
    java.io.FileWriter f = new java.io.FileWriter("forwardlog.txt", true);
    f.write(sender+"\n");
    f.close();
}
%>