CSharp Example

From SMS Wiki
Jump to: navigation, search

Below is sample code for C#. You are welcome to cut+paste this sample into your application, however, keep in mind that depending on your environment, some changes may be required.


Note: In order to run this sample, you need to reference System.Web.Extensions

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Xml;
using System.Xml.Serialization;
 
namespace TestUpsideRESTAPI
{
    class Program
    {
        const string APIEndPoint = "https://secureapi.upsidewireless.com"; // or "http://api.upsidewireless.com"
        const string APICredential_Token = "xxxxxxxx-8e4b-405b-998c-a580bf593b76";
        const string APICredential_Signature = "xxxxxxxxfyqQx95Gxxxxxxxx";
 
        private static readonly string SMSMessageUrl = string.Format("{0}/RESTv1/{1}/Message", APIEndPoint, APICredential_Token);
 
        static void Main(string[] args)
        {
            bool acceptJSON = false;
 
            Console.WriteLine("SMS Message URL: " + SMSMessageUrl + "\n");
 
            var request = WebRequest.Create(SMSMessageUrl) as HttpWebRequest;
           request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = acceptJSON ? "application/json" : "application/xml";
            request.UserAgent = "UpsideCSharpAgent";
            // use default timeout
            //request.Timeout = 100 * 1000; // in miliseconds
            //request.ReadWriteTimeout = 300 * 1000; // in miliseconds
 
            var smsPostData = "signature={0}";
            smsPostData += "&type={1}";
            smsPostData += "&message={2}";
            smsPostData += "&recipient={3}";
            smsPostData += "&encoding={4}";
 
            string type = "sms"; // see type list on wiki
            string message = "test sms message";
            string recipient = "16047891236"; // number in E.164 format
            string encoding = "7"; // 7, 8 or 16
 
            smsPostData = string.Format(smsPostData, APICredential_Signature, type, message, recipient, encoding);
            Console.WriteLine("Post Data: " + smsPostData + "\n");
            var data = Encoding.UTF8.GetBytes(smsPostData);
            request.ContentLength = data.Length;
 
            try
            {
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
 
                    HttpWebResponse response;
                    try
                    {
                        response = request.GetResponse() as HttpWebResponse;
                    }
                    catch (WebException ex)
                    {
                        response = ex.Response as HttpWebResponse;
                    }
 
                    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
 
                    Console.WriteLine("HTTP Response: " + responseString + "\n");
                    RestResponse restResponse = null;
                    if (acceptJSON)
                    {
                        JavaScriptSerializer JSS = new JavaScriptSerializer();
                        restResponse = JSS.Deserialize<RestResponse>(responseString);
                    }
                    else
                    {
                        var sreader = new StringReader(responseString);
                        var xmlreader = new XmlTextReader(sreader);
                        XmlSerializer serializer = new XmlSerializer(typeof(RestResponse));
                        restResponse = serializer.Deserialize(xmlreader) as RestResponse;
                    }
 
                    if (restResponse != null)
                    {
                        Console.WriteLine("Rest Response - HasException: " + restResponse.HasException);
                        Console.WriteLine("Rest Response - Token: " + restResponse.Token);
                        if (restResponse.HasException)
                        {
                            Console.WriteLine("Rest Response - RestException.ErrorCode: " + restResponse.RestException.ErrorCode);
                            Console.WriteLine("Rest Response - RestException.Message: " + restResponse.RestException.Message);
                            if (restResponse.RestException.Status != null)
                                Console.WriteLine("Rest Response - RestException.Status: " + restResponse.RestException.Status);
                        }
                        else
                        {
                            Console.WriteLine("Rest Response - SMSMessage.Status: " + restResponse.SMSMessage.Status);
                            Console.WriteLine("Rest Response - SMSMessage.Recipient: " + restResponse.SMSMessage.Recipient);
                            Console.WriteLine("Rest Response - SMSMessage.Body: " + restResponse.SMSMessage.Body);
                            Console.WriteLine("Rest Response - SMSMessage.Type: " + restResponse.SMSMessage.Type);
                            Console.WriteLine("Rest Response - SMSMessage.TrackingId: " + restResponse.SMSMessage.TrackingId);
                            if (restResponse.SMSMessage.Status == "REJECTED")
                                Console.WriteLine("Rest Response - SMSMessage.RejectReason: " + restResponse.SMSMessage.RejectReason);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Can't get rest response object");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in calling REST API - " + e.Message);
            }
 
            Console.ReadKey();
        }
 
    }
 
    public class RestResponse
    {
        public bool HasException { get; set; }
        public string Token { get; set; }
 
        public SMSMessage SMSMessage { get; set; }
 
        public RestException RestException { get; set; }
    }
 
    public class SMSMessage
    {
        public string Status { get; set; }
 
        public string Recipient { get; set; }
        public string Body { get; set; }
        public string Type { get; set; }
        public string TrackingId { get; set; }
        public string RejectReason { get; set; }
    }
 
    public class RestException
    {
        public int ErrorCode { get; set; }
        public string Message { get; set; }
 
        public string Status { get; set; }
    }
 
}