Esempio n. 1
0
    /// <summary>
    /// This function validates the input fields and if they are valid send sms api is invoked
    /// </summary>
    private void SendSms()
    {
        try
        {
            string        outBoundSmsJson    = string.Empty;
            List <string> destinationNumbers = new List <string>();
            if (!string.IsNullOrEmpty(address.Value))
            {
                string   addressInput      = address.Value;
                string[] multipleAddresses = addressInput.Split(',');
                foreach (string addr in multipleAddresses)
                {
                    if (addr.StartsWith("tel:"))
                    {
                        destinationNumbers.Add(addr);
                    }
                    else
                    {
                        string phoneNumberWithTel = "tel:" + addr;
                        destinationNumbers.Add(phoneNumberWithTel);
                    }
                }
                if (multipleAddresses.Length == 1)
                {
                    SendSMSDataForSingle outBoundSms = new SendSMSDataForSingle();
                    outBoundSms.outboundSMSRequest = new OutboundSMSRequestForSingle();
                    outBoundSms.outboundSMSRequest.notifyDeliveryStatus = chkGetOnlineStatus.Checked;
                    outBoundSms.outboundSMSRequest.address = destinationNumbers[0];
                    outBoundSms.outboundSMSRequest.message = message.SelectedValue;
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    outBoundSmsJson = javaScriptSerializer.Serialize(outBoundSms);
                }
                else
                {
                    SendSMSDataForMultiple outBoundSms = new SendSMSDataForMultiple();
                    outBoundSms.outboundSMSRequest = new OutboundSMSRequestForMultiple();
                    outBoundSms.outboundSMSRequest.notifyDeliveryStatus = chkGetOnlineStatus.Checked;
                    outBoundSms.outboundSMSRequest.address = destinationNumbers;
                    outBoundSms.outboundSMSRequest.message = message.SelectedValue;
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    outBoundSmsJson = javaScriptSerializer.Serialize(outBoundSms);
                }
            }
            else
            {
                sendSMSErrorMessage = "No input provided for Address";
                return;
            }



            string         sendSmsResponseData;
            HttpWebRequest sendSmsRequestObject = (HttpWebRequest)System.Net.WebRequest.Create(string.Empty + this.endPoint + this.sendSMSURL);
            sendSmsRequestObject.Method = "POST";
            sendSmsRequestObject.Headers.Add("Authorization", "Bearer " + this.accessToken);
            sendSmsRequestObject.ContentType = "application/json";
            sendSmsRequestObject.Accept      = "application/json";

            UTF8Encoding encoding  = new UTF8Encoding();
            byte[]       postBytes = encoding.GetBytes(outBoundSmsJson);
            sendSmsRequestObject.ContentLength = postBytes.Length;

            Stream postStream = sendSmsRequestObject.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            HttpWebResponse sendSmsResponseObject = (HttpWebResponse)sendSmsRequestObject.GetResponse();
            using (StreamReader sendSmsResponseStream = new StreamReader(sendSmsResponseObject.GetResponseStream()))
            {
                sendSmsResponseData = sendSmsResponseStream.ReadToEnd();
                JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
                sendSMSResponseData = new SendSMSResponse();
                sendSMSResponseData.outBoundSMSResponse = new OutBoundSMSResponse();
                sendSMSResponseData = (SendSMSResponse)deserializeJsonObject.Deserialize(sendSmsResponseData, typeof(SendSMSResponse));
                if (!chkGetOnlineStatus.Checked)
                {
                    Session["lastSentSMSID"] = sendSMSResponseData.outBoundSMSResponse.messageId;
                    messageId.Value          = Session["lastSentSMSID"].ToString();
                }
                sendSMSSuccessMessage = "Success";
                sendSmsResponseStream.Close();
            }
        }
        catch (WebException we)
        {
            string errorResponse = string.Empty;

            try
            {
                using (StreamReader sr2 = new StreamReader(we.Response.GetResponseStream()))
                {
                    errorResponse = sr2.ReadToEnd();
                    sr2.Close();
                }
            }
            catch
            {
                errorResponse = "Unable to get response";
            }

            sendSMSErrorMessage = errorResponse;
        }
        catch (Exception ex)
        {
            sendSMSErrorMessage = ex.ToString();
        }
    }
    /// <summary>
    /// This function validates the input fields and if they are valid send sms api is invoked
    /// </summary>
    private void SendSms()
    {
        try
        {
            string outBoundSmsJson = string.Empty;
            List<string> destinationNumbers = new List<string>();
            if (!string.IsNullOrEmpty(address.Value))
            {
                string addressInput = address.Value;
                string[] multipleAddresses = addressInput.Split(',');
                foreach (string addr in multipleAddresses)
                {
                    if (addr.StartsWith("tel:"))
                    {
                        destinationNumbers.Add(addr);
                    }
                    else
                    {
                        string phoneNumberWithTel = "tel:" + addr;
                        destinationNumbers.Add(phoneNumberWithTel);
                    }
                }
                if (multipleAddresses.Length == 1)
                {
                    SendSMSDataForSingle outBoundSms = new SendSMSDataForSingle();
                    outBoundSms.outboundSMSRequest = new OutboundSMSRequestForSingle();
                    outBoundSms.outboundSMSRequest.notifyDeliveryStatus = chkGetOnlineStatus.Checked;
                    outBoundSms.outboundSMSRequest.address = destinationNumbers[0];
                    outBoundSms.outboundSMSRequest.message = message.SelectedValue;
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    outBoundSmsJson = javaScriptSerializer.Serialize(outBoundSms);
                }
                else
                {
                    SendSMSDataForMultiple outBoundSms = new SendSMSDataForMultiple();
                    outBoundSms.outboundSMSRequest = new OutboundSMSRequestForMultiple();
                    outBoundSms.outboundSMSRequest.notifyDeliveryStatus = chkGetOnlineStatus.Checked;
                    outBoundSms.outboundSMSRequest.address = destinationNumbers;
                    outBoundSms.outboundSMSRequest.message = message.SelectedValue;
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    outBoundSmsJson = javaScriptSerializer.Serialize(outBoundSms);
                }
            }
            else
            {
                sendSMSErrorMessage = "No input provided for Address";
                return;
            }

            string sendSmsResponseData;
            HttpWebRequest sendSmsRequestObject = (HttpWebRequest)System.Net.WebRequest.Create(string.Empty + this.endPoint + this.sendSMSURL);
            sendSmsRequestObject.Method = "POST";
            sendSmsRequestObject.Headers.Add("Authorization", "Bearer " + this.accessToken);
            sendSmsRequestObject.ContentType = "application/json";
            sendSmsRequestObject.Accept = "application/json";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postBytes = encoding.GetBytes(outBoundSmsJson);
            sendSmsRequestObject.ContentLength = postBytes.Length;

            Stream postStream = sendSmsRequestObject.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            HttpWebResponse sendSmsResponseObject = (HttpWebResponse)sendSmsRequestObject.GetResponse();
            using (StreamReader sendSmsResponseStream = new StreamReader(sendSmsResponseObject.GetResponseStream()))
            {
                sendSmsResponseData = sendSmsResponseStream.ReadToEnd();
                JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
                sendSMSResponseData = new SendSMSResponse();
                sendSMSResponseData.outBoundSMSResponse = new OutBoundSMSResponse();
                sendSMSResponseData = (SendSMSResponse)deserializeJsonObject.Deserialize(sendSmsResponseData, typeof(SendSMSResponse));
                if (!chkGetOnlineStatus.Checked)
                {
                    Session["lastSentSMSID"] = sendSMSResponseData.outBoundSMSResponse.messageId;
                    messageId.Value = Session["lastSentSMSID"].ToString();
                }
                sendSMSSuccessMessage = "Success";
                sendSmsResponseStream.Close();
            }

        }
        catch (WebException we)
        {
            string errorResponse = string.Empty;

            try
            {
                using (StreamReader sr2 = new StreamReader(we.Response.GetResponseStream()))
                {
                    errorResponse = sr2.ReadToEnd();
                    sr2.Close();
                }
            }
            catch
            {
                errorResponse = "Unable to get response";
            }

            sendSMSErrorMessage= errorResponse;
        }
        catch (Exception ex)
        {
            sendSMSErrorMessage = ex.ToString();
        }
    }