Home > Code > General > C++ SMS source code on how to send SMS text messages using HTTP

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

by Aarav Gupta   on Jan 16, 2015   Category: General   |  Views: 890    |  Points: 25   |  Starter 

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! :)


using namespace std;

string encode(string url);

int main(int argc, char** argv)
// the SMS gateway's host
// and port
string host = "localhost";
int port = 9502;

// username
// and password
string username = "admin";
string password = "abc123";

// message
string message = "This is a test SMS.";

// originator's phone number
string originator = "+44555555555";

// recipient's phone number.
// to send the SMS to multiple recipients, separate them by using commas without spaces
string recipient = "+44333333333";

// preparing the HTTPRequest URL
stringstream url;
url << "/api?action=sendmessage&username=" << encode(username);
url << "&password=" << encode(password);
url << "&recipient=" << encode(recipient);
url << "&messagetype=SMS:TEXT&messagedata=" << encode(message);
url << "&originator=" << encode(originator);
url << "&responseformat=xml";

// create socket

// open connection and bind it to the socket
HINTERNET conn = InternetConnect(inet, host.c_str() , port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

// open the HTTP request
HINTERNET sess = HttpOpenRequest(conn, "GET", url.str().c_str(), "HTTP/1.1", NULL, NULL, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);

// check errors
int error = GetLastError();
if(error == NO_ERROR)
// send HTTP request
HttpSendRequest(sess, NULL, 0, NULL,0);

// receive HTTP response

int size = 1024;
char *buffer = new char[size + 1];
DWORD read;
int rsize = InternetReadFile(sess, (void *)buffer, size, &read);
string s = buffer;
s = s.substr(0, read);

// check status code
int pos = s.find("<statuscode>0</statuscode>");

// if statuscode is 0, write "Message sent." to output
// else write "Error."
if(pos > 0) cout << "Message sent." << endl;
else cout << "Error." << endl;


// encoding converts characters that are not allowed in a URL into character-entity equivalent
string encode(string url)
char *hex = "0123456789abcdef";
stringstream s;

for(unsigned int i = 0; i < url.length(); i++)
byte c = (char)url.c_str();
if( ('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z')
|| ('0' <= c && c <= '9') ){
s << c;
} else {
if(c == ' ') s << "%20";
s << '%' << (hex[c >> 4]) << (hex[c & 15]);

return s.str();

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts