C sharp with HttpListener to receive SMS messages

Download: HTTP-listener-console.zip

This guide gives you details about how to use C# application with HttpListener to receive SMS messages and to generate a response SMS messages. This solution can be used with either a GSM modem or with IP SMS. If you use modem, then it is an in-house solution, which means you do not need to sign up for an SMS service provided by SMS service providers over the Internet. You only need to purchase a GSM Modem with a datacable. If you wish to use an IP SMS, you need to make an agreement with an SMS service provider, and ask for IP address, port number, a username and a password.


In order to receive SMS messages with a built in webserver in a C# application, you need an SMS Gateway, like Ozeki NG - SMS Gateway, you need a GSM Modem and a data cable. First of all, you have to attach your GSM Modem with the data cable to the serial (RS232) port of your PC. After that, you have to configure the SMS gateway, so it can talk to the modem and forward the incoming messages to the C# application.

Figure 1 illustrates the architecture of your setup. On this figure, you can see the process of message sending, which is the following: the Mobile user sends an SMS message to the GSM Modem attached to the computer. Ozeki NG - SMS gateway collects this message from the modem and forwards it to the C# application with an HTTP request. The C# application generates an HTTP response to the request. This HTTP response contains the content of the response SMS message. This response SMS message will be forwarded to the Mobile user with the help of the the GSM Modem.

overview of sms gateway to asp.net architecture
Figure 1. - Overview of SMS Gateway to ASP.NET architecture

Configure your SMS Gateway

First of all, you need to install and configure the SMS gateway in order to get this setup running. Ozeki NG - SMS Gateway can be downloaded from www.ozekisms.com. Use this gateway because it is reliable, user-friendly, easy to configure and it is based on .NET, which means that it can be integrated well to your architecture. To make this SMS gateway work, you need to configure the GSM modem attached to your PC. This can be done easily by adding a GSM Modem service provider connection. Here are the steps:

Step 1.) Open 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.) Select the com port (usually COM1) on the GSM modem configuration form and click on autodetect to configure the modem

If you are finished with this, you need to configure an HTTP client user, that will forward the incoming messages to your C sharp application (Figure 2). This is an automated user, which is able to forward the messages to your application and process the HTTP responses.

installing an http client user
Figure 2 - Installing a Http Client user

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

Step 5.) For username provide "httpclient1"

Step 6.) For Target URL enter:$originator&receiver=$recipient&msgdata=$messagedata& recvtime=$receivedtime&msgid=$messageid

http target url setting
Figure 3 - HTTP Target URL setting

Step 7.) It is suggested to turn on detailed logging, which can be done by enabling the "Log low level communication" checkbox.

After these configuration steps, your SMS Gateway setup will look like this:

sms gateway setup
Figure 4 - SMS Gateway setup

Figure 4 - SMS Gateway setup

Build your C# application

Once you have the SMS Gateway configured, you can build your C# application. This application's built in webserver is HttpListener. HttpListener is a very efficient webserver available on Windows XP SP2 and Windows 2003 or later windows version such as Windows Vista. The HttpListener class is part of the System.Net package and it comes with .NET Framework 2.0.

If you take a look at the source code (Figure 5) you will see that as a first step of getting this solution running, you need to register the "" address as your URL prefix. Please note, that this URL is configured into the SMS Gateway HTTP Client user.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

namespace httplistener_console
    class Program
        static void Main(string[] args)
                HttpListener listener = new HttpListener();

                //The listeningURL will hold the listening URL
                string listeningURL = "http://+:8080/";
                Console.WriteLine("Ozeki HTTP server - Listening started at {0}", listeningURL);

                while (listener.IsListening)
                    HttpListenerContext context = listener.GetContext();

                        Thread t = new Thread(() =>
                            //Querying the sender address
                            string sender = context.Request.QueryString["sender"];
                            //"message" variable holds the response message
                            string message = "Thank you for your message";

                            string response = "{SMS:TEXT}{}{+1234657}{" + sender + "}{" + message + "}";
                            byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(response);
                            context.Response.ContentType = "text/html; charset=utf-8";
                            context.Response.OutputStream.Write(Buffer, 0, Buffer.Length);

                            //writing the sent message into the console
                            Console.WriteLine("Message sent to {0}. Data: {1}", sender, message);

                    catch (Exception e)

            catch (Exception e)

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 main 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.


This guide was a simple example of how to use C# application with a built in webserver to receive incoming messages and send responses. The guide showed how to configure an HTTP - SMS Gateway and demonstrated the capabilities of HttpListener class. This example application can be developed to several real-life solutions.

More information