C++ SMS source code on how to send SMS text messages using HTTP


/ Published in: C++
Save to your folder(s)

Hey Guys,

This is a short and straightforward article (or let’s say „tip” or „mini-review”) that presents how to send SMS messages from your own C++ (Cpp / C plus plus) application through HTTP. You will see, it is gonna be really easy. I will focus on the necessary code, but I would like to draw your attention to all the hardware and software requirements that are essentially needed for SMS messaging.

Briefly about SMS technology for better understanding

If you are proficient in SMS technology, just skip this paragraph and move on to the next one. So well, if you want to send SMS messages from your C++ application, first you need to connect your software and an SMS gateway to each other. Why? Because your application let you compose the SMS messages (including the text, the recipient’s phone number, etc.), but if you want to send out the SMS, you need to connect to the SMSC (Short Message Service Center that stores, forwards, converts and delivers SMS messages). An SMS gateway is able to connect to the SMSC of the Mobile Service Provider via SMPP IP SMS connection or a GSM modem, so actually the SMS gateway can send out your message. To establish connection between the SMS gateway and the C++ application, I used HTTP requests and responses. For sending SMS messages through HTTP, your SMS gateway should have a built-in webserver (e.g. Ozeki NG – www.ozekisms.com). The built-in webserver allows you to send SMS messages from your C++ application by calling an URL (HTTP Get) or using the HTTP Post method.

So that, all you need to have for sending SMS from your C++ application using HTTP is Microsoft Visual Studio for C++ development, an SMS gateway and my code snippet!

Okay and finally a few lines about testing. To test your software by sending out SMS message(s) actually, you need to install a GSM modem connection or an IP SMS connection. Buti f you have not contracted with any mobile service provider (e.g. AT&T or T-Mobile), you can test the SMS sending by simulating it (e.g. Ozeki NG provides „HTTP Server Connection” that can be used for this purpose). For more details related to the configuration of the SMS gateway, please contact the vendor of your SMS gateway.

Happy coding! :)


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. #include<sstream>
  3. #include<windows.h>
  4. #include<wininet.h>
  5.  
  6. using namespace std;
  7.  
  8. string encode(string url);
  9.  
  10. int main(int argc, char** argv)
  11. {
  12. // the SMS gateway's host
  13. // and port
  14. string host = "localhost";
  15. int port = 9502;
  16.  
  17. // username
  18. // and password
  19. string username = "admin";
  20. string password = "abc123";
  21.  
  22. // message
  23. string message = "This is a test SMS.";
  24.  
  25. // originator's phone number
  26. string originator = "+44555555555";
  27.  
  28. // recipient's phone number.
  29. // to send the SMS to multiple recipients, separate them by using commas without spaces
  30. string recipient = "+44333333333";
  31.  
  32. // preparing the HTTPRequest URL
  33. stringstream url;
  34. url << "/api?action=sendmessage&username=" << encode(username);
  35. url << "&password=" << encode(password);
  36. url << "&recipient=" << encode(recipient);
  37. url << "&messagetype=SMS:TEXT&messagedata=" << encode(message);
  38. url << "&originator=" << encode(originator);
  39. url << "&responseformat=xml";
  40.  
  41. // create socket
  42. HINTERNET inet = InternetOpen("Ozeki", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  43.  
  44. // open connection and bind it to the socket
  45. HINTERNET conn = InternetConnect(inet, host.c_str() , port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
  46.  
  47. // open the HTTP request
  48. HINTERNET sess = HttpOpenRequest(conn, "GET", url.str().c_str(), "HTTP/1.1", NULL, NULL, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
  49.  
  50. // check errors
  51. int error = GetLastError();
  52. if(error == NO_ERROR)
  53. {
  54. // send HTTP request
  55. HttpSendRequest(sess, NULL, 0, NULL,0);
  56.  
  57. // receive HTTP response
  58.  
  59. int size = 1024;
  60. char *buffer = new char[size + 1];
  61. DWORD read;
  62. int rsize = InternetReadFile(sess, (void *)buffer, size, &read);
  63. string s = buffer;
  64. s = s.substr(0, read);
  65.  
  66. // check status code
  67. int pos = s.find("<statuscode>0</statuscode>");
  68.  
  69. // if statuscode is 0, write "Message sent." to output
  70. // else write "Error."
  71. if(pos > 0) cout << "Message sent." << endl;
  72. else cout << "Error." << endl;
  73. }
  74.  
  75. system("pause");
  76. }
  77.  
  78.  
  79. // encoding converts characters that are not allowed in a URL into character-entity equivalent
  80. string encode(string url)
  81. {
  82. char *hex = "0123456789abcdef";
  83. stringstream s;
  84.  
  85. for(unsigned int i = 0; i < url.length(); i++)
  86. {
  87. byte c = (char)url.c_str()[i];
  88. if( ('a' <= c && c <= 'z')
  89. || ('A' <= c && c <= 'Z')
  90. || ('0' <= c && c <= '9') ){
  91. s << c;
  92. } else {
  93. if(c == ' ') s << "%20";
  94. else
  95. s << '%' << (hex[c >> 4]) << (hex[c & 15]);
  96. }
  97. }
  98.  
  99. return s.str();
  100. }

URL: http://www.ozekisms.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.