Advanced Services - SMS Connectivity

C# Sample application using HTTP to send a picture message

HSL Mobile Messaging

The following sample application written in C# sends a picture 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 for access to 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 = "sendnok";
String client_id = "<your client_id>";
String destaddr = "<some fully-qualified MSISDN>";
String type = "pg";
String content = "50340a232043726561746564206279205061696e742053686f702050726";
      content += "f20370a37322032380a0000000000000000000003ffffffffffffff0000";
      content += "0000000000000000e0000000000000000338000000000000000cee00000";
      content += "000000000131b01f81f8fe7c0006c06c0600618630000f001e060063023";
      content += "0000e000e0600630030000e000e0600618030000e000e060061c030000e";
      content += "000e060060f030000e000e07ffe07830000e000e0600601c30000e000e0";
      content += "600600e30000e000e0600600730000e000e0600600730000e000e060062";
      content += "0730080f001e0600630e301807c07c1f81fbf87ff801f1f000000000000";
      content += "000ffe0000000000000003f80000000000000000e000000000000000000";
      content += "0000000000000000003ffffffffffffff000000000000000000";
String text = "this is a test message";
String secret = "<your secret>";

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

picture_message_http_example_csharp.aspx


Copyright © Hay Systems Ltd 2004

Owner: support@haysystems.com 20 September 2004

Sample code link | Developers section link