|
Advanced Services - SMS Connectivity JAVA Sample application using HTTP |
![]() |
The following sample application written in Java sends an SMS message to a single mobile telephone. The application uses HTTP to communicate with HSL's systems and is run from the command line.
package com.haysystems.examples;
import java.net.*;
import java.io.*;
import java.security.*;
/**
* A simple example class that sends some text via the HSL HTTP gateway.
*
* @see HSL ADVANCED SERVICES SMS GATEWAY - HTTP INTERFACE (revision 4)
*/
public class HTTPExample {
public static void main(String[] args) {
// Storage for the data that will be extracted from the command-line.
String clientIdentifier = null;
String text = null;
String GSMNumber = null;
String secret = null;
// Process command-line parameters.
for(int i = 0; i < args.length; i++) {
if(args[i].startsWith("clientid=")) {
clientIdentifier = args[i].substring(args[i].indexOf("=") + 1);
} else if(args[i].startsWith("text=")) {
text = args[i].substring(args[i].indexOf("=") + 1);
} else if(args[i].startsWith("number=")) {
GSMNumber = args[i].substring(args[i].indexOf("=") + 1);
} else if(args[i].startsWith("secret=")) {
secret = args[i].substring(args[i].indexOf("=") + 1);
} else {
printUsage();
return;
}
}
// Check that we've actually got all 4 parameters.
if(clientIdentifier == null || text == null || GSMNumber == null || secret == null) {
printUsage();
return;
}
// Compute the MD5 digest for the message
String MD5Digest = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(secret.getBytes());
md.update(text.getBytes());
byte[] digest = md.digest(GSMNumber.getBytes());
for(int i = 0; i < digest.length; i++) {
String octet = Integer.toHexString( ((int)digest[i]) & 0xFF );
if( octet.length() == 1 ) {
MD5Digest += "0" + octet;
} else {
MD5Digest += octet;
}
}
} catch(NoSuchAlgorithmException nsae) {
System.err.println("HTTPExample: NoSuchAlgorithmException; " + nsae.getMessage());
return;
}
try {
// Set up the connection.
URL url = new URL("http://sms.hsl.uk.com/sendtxt/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
// Get the output stream and write the parameters
PrintStream out = new PrintStream(connection.getOutputStream());
out.print("clientid=" + clientIdentifier + "&text=" + text
+ "&gsmno=" + GSMNumber + "&key=" + MD5Digest);
out.close();
// Read the response
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String response = in.readLine();
in.close();
// Process the result.
if(response.equals("FAIL")) {
System.err.println("HTTPExample: Send failed.");
} else if(response.equals("FAIL AUTH")) {
System.err.println("HTTPExample: Send failed due to authentication failure.");
} else if(response.startsWith("SUCCESS")) {
System.out.println("HTTPExample: Send succeeded.\n (" + response + ")");
}
} catch(MalformedURLException mue) {
System.err.println("HTTPExample: MalformedURLException; " + mue.getMessage());
} catch(ProtocolException pe) {
System.err.println("HTTPExample: ProtocolException; " + pe.getMessage());
} catch(IOException ioe) {
System.err.println("HTTPExample: IOException; " + ioe.getMessage());
}
}
private static void printUsage() {
System.err.println("com.haysystems.examples.HTTPExample: usage...");
System.err.println(" client ID, text and gsm number required in format \"key=value\"");
System.err.println(" e.g. clientid=foo");
System.err.println(" valid keys are: secret, clientid, text and number");
System.err.println(" note: key ordering is unimportant");
}
}
Copyright © Hay Systems Ltd 2001-2008
Owner: support@haysystems.com 25 June 2007