C# SMS

On this page C# developers can learn how to use the HTTP protocol for SMS messaging with the Ozeki Message Server.

Introduction

HTTP is the primary transport mechanism for communicating with resources over the World Wide Web. Developers often use this protocol because of its simplicity and popularity, and also because it enables them to bypass firewalls and proxies. HTTP is a request/response protocol between clients and servers. Using the Ozeki Message Server you can easily send SMS messages with a simple HTTP request. You can also receive SMS messages using the Ozeki SMS Server by implementing a very simple web server.

Configuration

If you would only like to send messages using the HTTP protocol, you do not need to configure the Ozeki Message Server. Therefore, you may as well skip this paragraph. However, if you also want to receive SMS Messages, you need to configure the OZEKI Message Server to enable it to act as an HTTP Client. For this, you need to install and configure a HTTP Client plugin. To find out how to install and configure the HTTP Client plugin, check out the following page:

HTTP Client Plugin. In the appropriate tab, you need to set a base URL where the messages will be sent.
(Example: http://127.0.0.1:9400/getresponse.php?sender=$SENDER&
                 receiver=$RECEIVER&msg=$MSG)

By configuring the HTTP Client plugin, you instruct the Ozeki Message Server to forward all incoming SMS messages to the base URL with the specified parameters. As a result, a web server listening on the base URL can easily process the messages.

Sending SMS using HTTP request/response in C#

To send SMS using the HTTP protocol, you only need to create a web request to the Ozeki Message Server. You can also receive the results of your request with a web response. In C# you can achieve this with a few lines of code:

// prepare the web page we will be asking for
String
   getUri = @"http://127.0.0.1:9333/ozeki?action=sendMessage&login=admin&
   password=abc123&recepient=" + recepient.ToString() + @"&messageData=" +
   message.ToString();

WebRequest request = (WebRequest)WebRequest.Create(getUri);
// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//read the response
StreamReader responseReader =
             new StreamReader(response.GetResponseStream());
String resultmsg = responseReader.ReadToEnd();
responseReader.Close();

You have to create a special HTTP request to the OZEKI Message Server, which listens on the 9333 port by default. This request starts with the hostname and the name of the port the Ozeki Server listens on (127.0.0.1:9333 in our example). You need to request a virtual page called ozeki and send it the parameters, depending on the action you intend to perform. For sending SMS messages you have to use:

action=sendMessage,
login=[admins login],
password=[admins password],
recepient=[phone number of the recepient],
messageData=[the message you wish to send].
See more at : https://ozeki.hu/p_654-sms-api-for-webdevelopers-http-server.html

After creating the request and calling the GetResponse() method on the request, the SMS Message will be sent, and you can optionally read the response. The response should look like this:


      sendMessage
      
             <AcceptReport>
                <StatusCode>0</StatusCode>
                <StatusText>Message accepted for
delivery</StatusText>
                <MessageID>9294143</MessageID>
             </AcceptReport>
      

Receiving SMS using HTTP request/response in C#

To receive SMS messages using HTTP, you need to start a simple web server which is very easy in C# on Windows XP, SP2 or Server 2003. First, you need to create a new HttpListener object using the constructor of the HttpListener class . Use the Prefixes property to gain access to a specific Uniform Resource Identifier (URI), which will be the base URL.
(Note: Prefixes have to end in a forward slash "/".) Next, start this listener and write a callback method which will handle all requests to your server. The BeginGetContext() method begins an asynchronous (non-blocking) call to receive incoming client requests. For performance reasons, start 20 of them.

public class HttpServer
{
private HttpListener listener;

public HttpServer()
{
       listener = new HttpListener();
       listener.Prefixes.Add("http://127.0.0.1:9400/");//server url
       listener.Start();
       //Start 20 servers
       for (int x = 0; x < 20; x++)
       {
       listener.BeginGetContext(new AsyncCallback(ListenerCallback),
       this.listener);
       }
}

protected void ListenerCallback(IAsyncResult result)
{
       if (this.listener == null) return;
       HttpListenerContext context =
       this.listener.EndGetContext(result);
       //We setup a new context for the next request
       this.listener.BeginGetContext(new AsyncCallback(ListenerCallback),
       this.listener);
       this.ProcessRequest(context);
       context.Response.Close();
}

//Overridable method that can be used to implement a custom handler
protected virtual void ProcessRequest(HttpListenerContext context)
{
      string Sender = context.Request.QueryString["sender"];
      string Receiver = context.Request.QueryString["receiver"];
      string Msg = context.Request.QueryString["msg"];

      byte[] buffer =
System.Text.Encoding.UTF8.GetBytes("{SMS:TEXT}{}{+36205555224}
{+36201111245}{Thank you for the answer}" +
System.Environment.NewLine);
      context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}


Once your web server receives a HTTP request, the callback (ListenerCallback()) function will be called. This method will end the asynchronous operation with the EndGetContext() and immediately start a new context for the next request. The ProcessRequest() method can now process the incoming request. Each parameter of the request can be accessed by its name. For example, the message data will be in context.Request.QueryString["msg"].
(Note: If you want the output to be displayed on a GUI, consider you are on another thread, so watch out for illegal cross-thread calls.) After your server has received a request, you should send back a response which looks like this:

"{SMS:TEXT}{}{[sender number]}{[recipient number]}{[message text]}"       

In the request the sender's number is optional, but the recipient's number and the message text are mandatory.

More information