Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * Getting JSON body from JSON for demo. Then set the parameters. then convert it to JSON String
             * For production, please create proper class and OBJECTS from those
             */

            String  sms  = "{\"outboundSMSMessageRequest\":{\"address\":[\"tel:+94777339033\"],\"senderAddress\":\"tel:7555\",\"outboundSMSTextMessage\":{\"message\":\"Test Message\"},\"clientCorrelator\":\"123456\",\"receiptRequest\":{\"notifyURL\":\"http://128.199.174.220:1080/sms/report\",\"callbackData\":\"some-data-useful-to-the-requester\"},\"senderName\":\"87770\"}}";
            dynamic data = JsonConvert.DeserializeObject <dynamic>(sms);

            data.outboundSMSMessageRequest.address[0] = "tel:+" + txtMSISDN.Text.Trim();
            data.outboundSMSMessageRequest.outboundSMSTextMessage.message = txtMessage.Text;
            sms = JsonConvert.SerializeObject(data);


            String url = "https://ideabiz.lk/apicall/smsmessaging/v2/outbound/" + txtMSISDN.Text.Trim() + "/requests";

            textBox1.Text += "Sending SMS : " + url + Environment.NewLine;
            textBox1.Text += sms + Environment.NewLine;

            try
            {
                /*
                 * Sending API call to URL with Body. Result can get from IdeabizResponse class
                 */
                IdeabizResponse r = h.sendAPICall(url, IdeabizAPIAuth.REQUEST_METHOD.POST, sms, "application/json", "application/json");
                textBox1.Text += "Status : " + r.Status + ", Code : " + r.StatusCode + Environment.NewLine;
                textBox1.Text += r.Body + Environment.NewLine;
            }
            catch (Exception ex)
            {
                textBox1.Text += "Error: " + ex.Message + Environment.NewLine;
            }
            textBox1.Text += "============================================" + Environment.NewLine;
        }
Пример #2
0
        public IdeabizResponse sendRequest(String URL, REQUEST_METHOD method, String body, String contentType, String authorization, String accept)
        {
            IdeabizResponse ideabizResponse = new IdeabizResponse();

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(URL);

                request.ContentType = contentType;
                request.Accept      = accept;
                request.Headers["Authorization"] = authorization;

                if (method == REQUEST_METHOD.POST)
                {
                    var data = Encoding.ASCII.GetBytes(body);;

                    if (ContainsUnicodeCharacter(body))
                    {
                        data = Encoding.UTF8.GetBytes(body);
                    }


                    request.Method        = "POST";
                    request.ContentLength = data.Length;
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                ideabizResponse.StatusCode = (int)response.StatusCode;

                String responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                ideabizResponse.Body   = responseString;
                ideabizResponse.Status = "SUCCESS";
            }
            catch (WebException ex)
            {
                HttpWebResponse response = (HttpWebResponse)ex.Response;

                if (response == null)
                {
                    throw ex;
                }
                ideabizResponse.Status     = "ERROR";
                ideabizResponse.StatusCode = (int)response.StatusCode;
                ideabizResponse.Body       = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            return(ideabizResponse);
        }
        /// <summary>
        /// Send API call to ideabiz
        /// This will handle and inject access token
        /// This library need https://www.nuget.org/packages/Newtonsoft.Json/
        /// </summary>
        /// <param name="URL">Request full URL (eg : https://ideabiz.lk/apicall/smsmessaging/v2/94777123456/request</param>
        /// <param name="method">IdeabizAPIAuth.REQUEST_METHOD GET OR POST</param>
        /// <param name="body">plain text body. JSON string or urlencoded body</param>
        /// <param name="contentType">Content type, eg: application/json</param>
        /// <param name="accept">Accept content type : eg application/json</param>
        /// <returns>Ideabiz Response contain status, statusCode, Body</returns>
        public IdeabizResponse sendAPICall(String URL, IdeabizAPIAuth.REQUEST_METHOD method, String body, String contentType, String accept)
        {
            IdeabizResponse response = idabizAuth.sendRequest(URL, method, body, contentType, "Bearer " + idabizAuth.ACCESS_TOKEN, accept);

            if (response.Status.Equals("ERROR") && response.StatusCode == 401 && (response.Body.Contains("Expired") || response.Body.Contains("Inactive")))
            {
                IdabizAuth.renewToken();
                response = idabizAuth.sendRequest(URL, method, body, contentType, "Bearer " + idabizAuth.ACCESS_TOKEN, accept);
            }

            return(response);
        }
Пример #4
0
        /*
         * Once token expired, it will renew and save from this method
         */
        public void renewToken()
        {
            IdeabizResponse response = sendRequest(TOKEN_API + "?grant_type=refresh_token&refresh_token=" + REFRESH_TOKEN + "&scope=PRODUCTION", REQUEST_METHOD.POST, " ", "application/x-www-form-urlencoded", "Basic " + Base64Encode(CONSUMER_KEY + ":" + CONSUMER_SECRET), "application/json");

            if (response.Status.Equals("SUCCESS"))
            {
                Dictionary <string, string> data = JsonConvert.DeserializeObject <Dictionary <string, string> > (response.Body);

                data.TryGetValue("access_token", out ACCESS_TOKEN);
                data.TryGetValue("refresh_token", out REFRESH_TOKEN);
                saveSettings();
            }
            else
            {
                throw new Exception("Authentication error");
            }
        }
Пример #5
0
        private void button3_Click(object sender, EventArgs e)
        {
            String url = "https://ideabiz.lk/apicall/location/v1/queries/location?address=" + txtMSISDN.Text.Trim() + "&requestedAccuracy=1000";

            textBox1.Text += "Sending SMS : " + url + Environment.NewLine;

            try
            {
                /*
                 * Sending API call to URL with Body. Result can get from IdeabizResponse class
                 */
                IdeabizResponse r = h.sendAPICall(url, IdeabizAPIAuth.REQUEST_METHOD.GET, null, "application/x-www-form-urlencoded", "application/json");
                textBox1.Text += "Status : " + r.Status + ", Code : " + r.StatusCode + Environment.NewLine;
                textBox1.Text += r.Body + Environment.NewLine;
            }
            catch (Exception ex)
            {
                textBox1.Text += "Error: " + ex.Message + Environment.NewLine;
            }

            textBox1.Text += "============================================" + Environment.NewLine;
        }
        public  IdeabizResponse sendRequest(String URL, REQUEST_METHOD method, String body, String contentType,String authorization, String accept)
        {
            IdeabizResponse ideabizResponse = new IdeabizResponse();

            try
            {

                var request = (HttpWebRequest)WebRequest.Create(URL);

                request.ContentType = contentType;
                request.Accept = accept;
                request.Headers["Authorization"] = authorization;

                if (method == REQUEST_METHOD.POST)
                {
                    var data = Encoding.ASCII.GetBytes(body); ;

                    if (ContainsUnicodeCharacter(body))
                    {
                        data = Encoding.UTF8.GetBytes(body);
                    }


                    request.Method = "POST";
                    request.ContentLength = data.Length;
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                ideabizResponse.StatusCode = (int)response.StatusCode ;

                String responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                ideabizResponse.Body = responseString;
                ideabizResponse.Status = "SUCCESS";


                

            }
            catch (WebException ex)
            {
                HttpWebResponse response = (HttpWebResponse)ex.Response;

                if(response == null)
                {
                    throw ex;
                }
                ideabizResponse.Status = "ERROR";
                ideabizResponse.StatusCode = (int)response.StatusCode;
                ideabizResponse.Body =new StreamReader(response.GetResponseStream()).ReadToEnd();


            }
         return ideabizResponse;

        }