Example #1
0
        static void PerformAsynchronousHTTPRequestCallback(IAsyncResult asyncResults)
        {
            string webResponseString;
            var    asyncStateObject = (asyncResults.AsyncState as AsyncHTTPRequestStateObject);

            try
            {
                if (asyncStateObject != null)
                {
                    var webResponse = asyncStateObject.Request.GetResponse();

                    using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
                        webResponseString = streamReader.ReadToEnd();

                    webResponse.Close();

                    UpdateHTTPTransaction(asyncStateObject.TransactionID, null, null, null, webResponseString, null);

                    string clientMessage   = string.Empty;
                    string clientReference = string.Empty;

                    XmlDocument responseXMLDoc = new XmlDocument();
                    responseXMLDoc.LoadXml(webResponseString);

                    if (TryGetXMLElement(responseXMLDoc, new string[] { "SM", "Message" }, out clientMessage))
                    {
                        UpdateHTTPTransaction(asyncStateObject.TransactionID, null, null, clientMessage, null, null);
                    }

                    if (TryGetXMLElement(responseXMLDoc, new string[] { "SessionReference", "RefNo" }, out clientReference))
                    {
                        UpdateHTTPTransaction(asyncStateObject.TransactionID, null, clientReference, null, null, null);
                    }

                    string response;
                    if (asyncStateObject.SendHTTPResponseBySMS && !String.IsNullOrEmpty(clientMessage))
                    {
                        MenuCommunication.SendCommunication(asyncStateObject.USSDCampaignID, asyncStateObject.MenuID, asyncStateObject.USSDTransactionID, MenuItem.CommunicationType.SMS, asyncStateObject.MobileNetworkID, asyncStateObject.MobileNumber, clientMessage, asyncStateObject.USSDNumber, false, false, out response);
                    }
                }
            }
            catch (Exception ex)
            {
                if (asyncStateObject != null && asyncStateObject.TransactionID > 0)
                {
                    UpdateHTTPTransaction(asyncStateObject.TransactionID, null, null, null, null, ex.ToString());
                }
            }
        }
Example #2
0
        public static bool SendCommunication(int ussdCampaignID, int menuID, long ussdTransactionID, MenuItem.CommunicationType communicationType, int mobileNetworkID, string mobileNumber, string communicationText, string ussdNumber, bool waitForResponse, bool sendHTTPResponseBySMS, out string response)
        {
            bool hasSent = false;

            try
            {
                response = null;
                switch (communicationType)
                {
                case MenuItem.CommunicationType.SM2HTTP:
                    long transactionID = -1;
                    try
                    {
                        if (AddHTTPTransaction(ussdCampaignID, ussdTransactionID, menuID, mobileNumber, null, out transactionID))
                        {
                            StringBuilder url = new StringBuilder(communicationText);
                            url.AppendFormat("&MSISDN={0}&DESTADDRESS={1}&REFERENCE={2}", HttpUtility.UrlEncode(mobileNumber), HttpUtility.UrlEncode(ussdNumber), HttpUtility.UrlEncode(transactionID.ToString()));

                            UpdateHTTPTransaction(transactionID, url.ToString(), null, null, null, null);
                            WebResponse webResponse       = null;
                            string      webResponseString = string.Empty;
                            if (!waitForResponse)
                            {
                                var webRequest = HttpWebRequest.Create(url.ToString());
                                webRequest.Method = "GET";

                                var asyncStateObject = new AsyncHTTPRequestStateObject()
                                {
                                    Request                 = webRequest
                                    , TransactionID         = transactionID
                                    , MenuID                = menuID
                                    , MobileNetworkID       = mobileNetworkID
                                    , MobileNumber          = mobileNumber
                                    , USSDCampaignID        = ussdCampaignID
                                    , USSDNumber            = ussdNumber
                                    , USSDTransactionID     = ussdTransactionID
                                    , SendHTTPResponseBySMS = sendHTTPResponseBySMS
                                };


                                IAsyncResult asyncResult = webRequest.BeginGetResponse(new AsyncCallback(PerformAsynchronousHTTPRequestCallback), asyncStateObject);

                                System.Threading.ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new System.Threading.WaitOrTimerCallback(AsynchronousHTTPRequestTimeoutCallback), asyncStateObject, asyncStateObject.Request.Timeout, true);
                                return(true);
                            }
                            else
                            {
                                try
                                {
                                    var webRequest = HttpWebRequest.Create(url.ToString());
                                    webRequest.Method = "GET";
                                    webResponse       = webRequest.GetResponse();

                                    using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
                                        webResponseString = streamReader.ReadToEnd();

                                    UpdateHTTPTransaction(transactionID, null, null, null, webResponseString, null);
                                }
                                finally
                                {
                                    if (webResponse != null)
                                    {
                                        webResponse.Close();
                                    }
                                }
                                string clientMessage   = string.Empty;
                                string clientReference = string.Empty;

                                XmlDocument responseXMLDoc = new XmlDocument();
                                responseXMLDoc.LoadXml(webResponseString);

                                if (TryGetXMLElement(responseXMLDoc, new string[] { "SM", "Message" }, out clientMessage))
                                {
                                    response = clientMessage;
                                    UpdateHTTPTransaction(transactionID, null, null, clientMessage, null, null);
                                }
                                if (TryGetXMLElement(responseXMLDoc, new string[] { "SessionReference", "RefNo" }, out clientReference))
                                {
                                    UpdateHTTPTransaction(transactionID, null, clientReference, null, null, null);
                                }

                                if (sendHTTPResponseBySMS && !String.IsNullOrEmpty(clientMessage))
                                {
                                    MenuCommunication.SendCommunication(ussdCampaignID, menuID, ussdTransactionID, MenuItem.CommunicationType.SMS, mobileNetworkID, mobileNumber, clientMessage, ussdNumber, false, false, out response);
                                    response = null;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (transactionID > 0)
                        {
                            UpdateHTTPTransaction(transactionID, null, null, null, null, ex.ToString());
                        }
                    }
                    break;

                case MenuItem.CommunicationType.SMS:
                case MenuItem.CommunicationType.MMS:
                    CQueueWrapper commsSendQueue = null;

                    if (communicationType == MenuItem.CommunicationType.MMS)
                    {
                        commsSendQueue = new CQueueWrapper("EXACTUSSDMMSCOMMUNICATIONQ");
                    }
                    else if (communicationType == MenuItem.CommunicationType.SMS)
                    {
                        commsSendQueue = new CQueueWrapper("EXACTUSSDSMSCOMMUNICATIONQ");
                    }

                    CXMLCapsule commsCapsule = new CXMLCapsule();
                    commsCapsule.SetParameter("USSDTransactionID", ussdTransactionID.ToString(), true);
                    commsCapsule.SetParameter("USSDCampaignID", ussdCampaignID.ToString(), true);
                    commsCapsule.SetParameter("USSDMenuID", menuID.ToString(), true);
                    commsCapsule.SetParameter("MobileNumber", mobileNumber, true);
                    commsCapsule.SetParameter("MobileNetworkID", mobileNetworkID.ToString(), true);
                    commsCapsule.SetParameter("CommunicationText", communicationText, true);
                    commsCapsule.SetParameter("CommunicationTypeID", Convert.ToInt32(communicationType).ToString(), true);
                    commsCapsule.SetParameter("USSDNumber", ussdNumber, true);
                    if (commsSendQueue != null)
                    {
                        commsSendQueue.Send(commsCapsule);
                        hasSent = true;
                    }
                    break;
                }
            }
            finally
            {
            }
            return(hasSent);
        }