public async Task <string> SendMessage(string PhoneNumber, string Message) { string finalresponse = ""; if (PhoneNumber == "" || Message == "") { finalresponse = "Invalid Parameters"; } else { String strConnString = "Data Source=192.168.10.6;Initial Catalog=SMS;Integrated Security=False;user id=OnlineServices;password=Whanganui497"; SqlConnection con = new SqlConnection(strConnString); SqlCommand cmd = new SqlCommand("GET_PARAMETER", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@parameter", SqlDbType.VarChar).Value = "IPAddress"; cmd.Connection = con; try { con.Open(); //SqlDataReader dr = cmd.ExecuteReader(); IPAddress = cmd.ExecuteScalar().ToString(); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(ex.InnerException); throw ex; } finally { con.Close(); } cmd.Parameters.Clear(); cmd.Parameters.Add("@parameter", SqlDbType.VarChar).Value = "Port"; cmd.Connection = con; try { con.Open(); //SqlDataReader dr = cmd.ExecuteReader(); Port = cmd.ExecuteScalar().ToString(); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(ex.InnerException); throw ex; } finally { con.Close(); } using (var client = new HttpClient()) { string url = ConstructBaseUri(); client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", UserName, Password)))); } var postData = new List <KeyValuePair <string, string> >(); postData.Add(new KeyValuePair <string, string>("to", PhoneNumber)); postData.Add(new KeyValuePair <string, string>("message", Message)); HttpContent content = new FormUrlEncodedContent(postData); HttpResponseMessage response = await client.PostAsync(MessagesUrlPath, content); if (response.IsSuccessStatusCode) { PostMessageResponse result = await response.Content.ReadAsAsync <PostMessageResponse>(); if (result.IsSuccessful) { finalresponse = result.ToString(); } else { finalresponse = result.Description; } } else { finalresponse = response.ToString(); } } } return(finalresponse); }
private async void SendMessage() { if (string.IsNullOrEmpty(txtIPAddress.Text)) { MessageBox.Show("Please enter an IP address", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); txtIPAddress.Focus(); return; } if (string.IsNullOrEmpty(txtContact.Text) || string.IsNullOrEmpty(txtMessage.Text)) { MessageBox.Show("Please enter the contact name/number and the message", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); txtContact.Focus(); return; } try { btnSendMessage.Enabled = false; using (var client = new HttpClient()) { string url = ConstructBaseUri(); client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text)))); } var postData = new List <KeyValuePair <string, string> >(); postData.Add(new KeyValuePair <string, string>("to", txtContact.Text)); postData.Add(new KeyValuePair <string, string>("message", txtMessage.Text)); HttpContent content = new FormUrlEncodedContent(postData); HttpResponseMessage response = await client.PostAsync(MessagesUrlPath, content); if (response.IsSuccessStatusCode) { PostMessageResponse result = await response.Content.ReadAsAsync <PostMessageResponse>(); if (result.IsSuccessful) { txtOutput.Clear(); AddToOutput(result.ToString()); AddToOutput(""); } else { MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { btnSendMessage.Enabled = true; } }