Jump to content


Photo

How to build a C sharp application with HttpListener to receive SMS messages

sms gateway application code c# c sharp opinion

  • Please log in to reply
No replies to this topic

#1 maxh69

maxh69

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 26 October 2012 - 06:17 AM

Hi all,
I need a C# application to receive SMS messages, and I found a source code at the official site of Ozeki NG SMS Gateway with some information on HTTPListener, as well. Could you give me your opinion on:
1. The source code itself: whether it would work.
2. Ozeki NG SMS Gateway: whether it really is so reliable and robust as I keep reading it everywhere.
3. HTTPListener: if it would be a good choice as a webserver.
Thank you for your answers, and here’s what I found at http://www.ozekisms....ex.php?owpn=696
Once you have an 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 you will see that as a first step of getting this solution running, you need to register the "http://127.0.0.1:8080/" 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)
		{
			try
			{
				HttpListener listener = new HttpListener();
				//The listeningURL will hold the listening URL
				string listeningURL = "http://+:8080/";
				listener.Prefixes.Add(listeningURL);
				listener.Start();
				Console.WriteLine("Ozeki HTTP server - Listening started at {0}", listeningURL);
				while (listener.IsListening)
				{
					HttpListenerContext context = listener.GetContext();
					try
					{
						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);
							context.Response.Close();
							//writing the sent message into the console
							Console.WriteLine("Message sent to {0}. Data: {1}", sender, message);
							Console.ReadLine();
						});
						t.Start();
					}
					catch (Exception e)
					{
						Console.WriteLine(e.Message);
					}
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
	}
}

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.

Any answers would be appreciated.

Max




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users