|
Advanced Services - SMS Connectivity Java Sample application using HTTP to send an SMS text message |
![]() |
The following sample application written in Java sends an SMS text 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 SuperSimpleHTTPExample {
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 = "sendtxt";
String client_id = "<your client_id>";
String text = "this is a test message";
String destaddr = "<some fully-qualified MSISDN>";
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;
url += "/?client_id=" + client_id + "&text=";
url += URLEncoder.encode(text, "UTF-8") + "&destaddr="+ destaddr;
url += "&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();
}
}
}
SuperSimpleHTTPExample.java
Copyright © Hay Systems Ltd 2009
Owner: support@haysystems.com 20 September 2004