Advanced Services - SMS Connectivity

Java Sample application using HTTP to send an EMS message

HSL Mobile Messaging

The following sample application written in Java sends an EMS message to a single mobile telephone. The application uses HTTP to communicate with HSL's systems. To send to more than one mobile telephone at a time, separate each mobile number in the destaddr field by a comma (e.g. "447968000111,447720000111,3378100100").

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

public class SuperSimpleEMSHTTPExample {
    public static void main(String[] args) {
        try {
            // These variables fill out the various parts described in the
            // interface specification
            String host = "sms.haysystems.com";
            String port = "80";
            String action = "submitsm";
            String client_id = "<your client_id>";
            String destaddr = "<some fully-qualified MSISDN>";
            String shortmessage = "7C0C7A00424547494E3A494D454C4F44590D0A56455"
                + "253494F4E3A312E320D0A464F524D41543A434C415353312E300D0A4D45"
                + "4C4F44593A6433236333643323633364337232643323633364336533236"
                + "63365332366336733613323673361332367336133723261332367336133"
                + "0D0A454E443A494D454C4F44590D0A";
            String password = "<your password>";

            // Build and encode the URL.
            // Note we are careful to URL-encode the message content as it is
            // the only part of the URL likely to contain reserved characters.
            String url = "http://" + host + ":" + port + "/" + action
                       + "/?client_id=" + client_id + "&destaddr="+ destaddr
                       + "&esm=64&shortmessage=" + shortmessage + "&password="
                       + password;

            // Make a URL object, ready for the call
            URL urlObject = new URL(url);

            // Actually make a connection
            HttpURLConnection connection =
                (HttpURLConnection)urlObject.openConnection();
            connection.setDoInput(true);
            connection.connect();

            // Check the response
            int responseCode = connection.getResponseCode();
            if(responseCode == 200) {
                // Read the server's response
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));

                System.out.println("Submission result: " + in.readLine());
                in.close();

            } else {
                // There was an error
                System.err.println("Server responded with code " + responseCode);
            }

        } catch(Exception e) {
            System.err.println("Unable to make HTTP request due to an exception:");
            e.printStackTrace();
        }
    }
}

SuperSimpleEMSHTTPExample.java


Copyright © Hay Systems Ltd 2004

Owner: support@haysystems.com 20 September 2004

Sample code link | Developers section link