Guest User

Untitled

a guest
Nov 28th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5.  
  6. public class SendSMS
  7. {
  8. public static void Send(string Message,string PhoneNumber)
  9. {
  10. // This URL is used for sending messages
  11. string myURI = "https://api.bulksms.com/v1/messages";
  12.  
  13. // change these values to match your own account
  14. string myUsername = "";
  15. string myPassword = "";
  16.  
  17. // the details of the message we want to send
  18. string myData = "{to: \""+PhoneNumber + "\", body:\""+ Message + "!\"}";
  19.  
  20. // build the request based on the supplied settings
  21. var request = WebRequest.Create(myURI);
  22.  
  23. // supply the credentials
  24. request.Credentials = new NetworkCredential(myUsername, myPassword);
  25. request.PreAuthenticate = true;
  26. // we want to use HTTP POST
  27. request.Method = "POST";
  28. // for this API, the type must always be JSON
  29. request.ContentType = "application/json";
  30.  
  31. // Here we use Unicode encoding, but ASCIIEncoding would also work
  32. var encoding = new UnicodeEncoding();
  33. var encodedData = encoding.GetBytes(myData);
  34.  
  35. // Write the data to the request stream
  36. var stream = request.GetRequestStream();
  37. stream.Write(encodedData, 0, encodedData.Length);
  38. stream.Close();
  39.  
  40. // try ... catch to handle errors nicely
  41. try
  42. {
  43. // make the call to the API
  44. var response = request.GetResponse();
  45.  
  46. // read the response and print it to the console
  47. var reader = new StreamReader(response.GetResponseStream());
  48. Console.WriteLine(reader.ReadToEnd());
  49. }
  50. catch (WebException ex)
  51. {
  52. // show the general message
  53. Console.WriteLine("An error occurred:" + ex.Message);
  54.  
  55. // print the detail that comes with the error
  56. var reader = new StreamReader(ex.Response.GetResponseStream());
  57. Console.WriteLine("Error details:" + reader.ReadToEnd());
  58. }
  59. }
  60. }
Add Comment
Please, Sign In to add comment