ASP.NET with HttpListener to receive SMS messages

Download: VB.NET-SMS-Gateway-Source.zip

This guide gives you detailed instructions on how to use ASP.NET with HttpListener to receive SMS messages and to generate a response SMS messages. This solution is an in-house solution, which means you can set it up in your own computer if you purchase a GSM Modem with a datacable. You do not have to sign up for an SMS service provided by an SMS service provider over the Internet.

Introduction

To be able to receive SMS messages with a built in webserver in an ASP.NET application, you need an SMS Gateway, like Ozeki NG - SMS Gateway, you need a GSM Modem and a data cable. To start, you have to attach your GSM Modem with the data cable to the serial (RS232) port of your PC. Them you have to configure the SMS gateway to talk to the modem and to forward the incoming messages to the ASP.NET application.

Figure 1 illustrates the architecture of your setup. If you take a look at this diagram, you will see the process. The Mobile user sends an SMS message to the GSM Modem. Ozeki NG will collect this message from the modem and will forward it to the ASP.NET application using an HTTP request. The ASP.NET application will generate an HTTP response to the request. This HTTP response contains the text of the response SMS message. The response SMS message will be forwarded to the Mobile user through the GSM Modem.

the architect of sending and sms from the gateway to asp.net
Figure 1. - SMS Gateway to ASP.NET architecture

Configure your SMS Gateway

To get this setup running, the first step is to install and configure the SMS Gateway. The SMS Gateway we use in this article is the Ozeki NG - SMS Gateway it can be downloaded from www.ozekisms.com. We chose this SMS Gateway, because it is very reliable, easy to configure and it is based on .NET which means it integrates well into our architecture. In the SMS gateway first you should configure the GSM modem attached to your PC. This can be done by adding a GSM Modem service provider connection. Detailed steps:

Step 1.) Open http://127.0.0.1:9501 in Internet Explorer, login with admin/abc123

Step 2.) In the "Service provider connections" menu click on "Add service provider connection", then select "GSM Modem Connection" and click "Install"

Step 3.) On the GSM modem configuration form, select the com port (usually COM1) and click on autodetect to configure your modem

The next step is to configure an HTTP Client user, that will forward the incoming messages to your ASP.NET application. This automated user will not only forward the messages to your application, but will also process the HTTP responses.

how to install an http client user
Figure 2 - Installing a Http Client user

Step 4.) To install the HTTP Client user, go into the "Users and applications" menu click on "Add user", the select "HTTPClient user" and click on "install"

Step 5.) For username provide "httpclient1"

Step 6.) For Target URL enter:

http://localhost:9800/getresponse.asp?sender=$originator&receiver=$recipient&msgdata=$messagedata&recvtime=$receivedtime&msgid=$messageid

setting up a target url
Figure 3 - HTTP Target URL setting

Step 7.) In the configuration form I suggest you to turn on detailed logging. You can do this by enabling the checkbox: "Log low level communication."

After you have followed these steps, your SMS Gateway setup will look like this:

setting up the sms gateway
Figure 4 - SMS Gateway setup

Build your ASP.NET application

Once you have the SMS Gateway configured, you can build your ASP.NET application. This application uses HttpListener as the built int webserver. HttpListener is a very efficient webserver available on Windows XP SP2 and Windows 2003 or later windows version such as Windows 2012. The HttpListener class is part of the System.Net package and it comes with .NET Framework 3.5.

If you read the source code (Figure 5) you will see that the first step to get this solution running is to register the "http://127.0.0.1:9800/" address as your URL prefix. Please note, that we have configure this URL into the SMS Gateway HTTP Client user.

After we have registered the URL prefix, we have started 20 asynchronous callback servers by calling listener.BeginGetContext. This means that we can serve twenty incoming SMS messages at the same time.

Imports System.Net

Module Module1

    Dim listener As HttpListener

    Sub Main()
        listener = New HttpListener
        listener.Prefixes.Add("http://127.0.0.1:9800/")
        listener.Start()

        Dim i As Integer
        For i = 1 To 20
            listener.BeginGetContext(New AsyncCallback(AddressOf request),
            listener)
        Next

        Console.WriteLine("The built in HTTP Server is accepting incoming
        SMS messages")
        Console.ReadLine()
    End Sub

    Private Sub request(ByVal result As IAsyncResult)
        If listener Is Nothing Then Return

        Dim context As HttpListenerContext
        context = listener.EndGetContext(result)
        listener.BeginGetContext(New AsyncCallback(AddressOf request), listener)
        processrequest(context)
        context.Response.Close()
    End Sub

    Private Sub processrequest(ByVal context As HttpListenerContext)
        Dim sender As String
        sender = context.Request.QueryString("sender")
        Dim msg As String
        msg = context.Request.QueryString("msg")
        MsgBox(sender + " has sent the following SMS: " + msg)

        Dim response As String
        response = "{SMS:TEXT}{}{+1234657}{" + sender + "}{Thank you for your
        message}"

        Dim Buffer() As Byte = {}
        Buffer = System.Text.Encoding.UTF8.GetBytes(response)
        context.Response.ContentLength64 = Buffer.Length
        context.Response.ContentType = "text/html; charset=utf-8"
        context.Response.OutputStream.Write(Buffer, 0, Buffer.Length)
        context.Response.Close()
    End Sub

End Module

Figure 5 - Source code for receiving SMS messages through HTTP

The request subrouting is called asynchronously when an incoming SMS arrives. This sub is very simple, it calls the process request function to serve the requests.

The process request subroutine has two parts the first part reads the message from the HTTP requests and displays a MessageBox. The second part generates a response by writing to the Outputstream. The output will be encoded in UTF8 to support international characters. This is achieved by using UTF 8 encoding in the System.Text.Encoding.UTF8.GetBytes statement.

Summary

In this guide you could read a simple example on how to use ASP.NET with a built in webserver to receive incoming SMS messages and to send a response to these messages. The configuration of an HTTP - SMS Gateway has been shown and the capabilities of the HttpListener class has been demonstrated. The example application can be developed to server real life solutions.

More information