|
Advanced Services - SMS Connectivity C# Sample application using HTTP to send an EMS message |
![]() |
The following sample application written in C# 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 Namespace="System.Text" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" %>
<html>
<head>
<title>Simple C# (ASP.NET) example of EMS via the HSL HTTP interface</title>
</head>
<body>
<%
// 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 = "7C0C7A00424547494E3A494D454C4F44590D0A56455253494F4E3A3"
+ "12E320D0A464F524D41543A434C415353312E300D0A4D454C4F4459"
+ "3A64332363336433236333643372326433236333643365332366336"
+ "5332366336733613323673361332367336133723261332367336133"
+ "0D0A454E443A494D454C4F44590D0A";
String secret = "<your secret>";
// Compute the MD5 hash
MD5 md5 = new MD5CryptoServiceProvider();
Encoding encoding = Encoding.ASCII;
String hashInput = secret + shortmessage + destaddr;
byte[] hashInputBytes = encoding.GetBytes(hashInput.ToCharArray());
byte[] rawHash = md5.ComputeHash(hashInputBytes);
// Convert the raw bytes into a string
String hash = "";
for(int i = 0; i < rawHash.GetLength(0); i++) {
String coded = Convert.ToString(rawHash[i], 16);
if(coded.Length == 1) {
coded = "0" + coded;
}
hash += coded;
}
// Build and encode the URL.
String url = "http://" + host + ":" + port + "/" + action + "/?client_id="
+ client_id + "&destaddr="+ destaddr + "&esm=64&shortmessage="
+ shortmessage + "&key=" + hash;
// Create an HTTP request
WebRequest httpRequest = WebRequest.Create(url);
WebResponse httpResponse = httpRequest.GetResponse();
Stream stream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
// Make the request and print the result
Response.Write("<p>Submission result: ");
Response.Write(reader.ReadToEnd());
Response.Write("</p>");
reader.Close();
%>
</body>
</html>
ems_http_example_csharp.aspx
Copyright © Hay Systems Ltd 2009
Owner: support@haysystems.com 20 September 2004