Advanced Services - SMS Connectivity

C# Sample application using HTTP to send an SMS text message

HSL Mobile Messaging

The following sample application written in C# 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 Namespace="System.Text" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" %>

<html>
<head>
    <title>Simple C# example of text 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 = "sendtxt";
String client_id = "<your client_id>";
String text = "this is a test message";
String destaddr = "<some fully-qualified MSISDN>";
String secret = "<your secret>";

// Compute the MD5 hash
MD5 md5 = new MD5CryptoServiceProvider();
Encoding encoding = Encoding.ASCII;
String hashInput = secret + text + 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.
// 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 + "&text=" + Server.UrlEncode(text) + "&destaddr=" + destaddr
    + "&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>

simple_http_example_csharp.aspx


Copyright © Hay Systems Ltd 2004

Owner: support@haysystems.com 20 September 2004

Sample code link | Developers section link