Using the HTTP C++ SMS API
This is an example for Visual Studio 2008 developers on how to send SMS from C++ applications using HTTP request. The example is based on the Ozeki HTTP SMS API. For a higher performance C++ SMS source code, please visit the C++ SMS SDK that uses a TCP/IP socket to send and receive SMS messages.
Download: | cpp-sms-source-http.zip |
Introduction
The Ozeki HTTP SMS Api is an excellent solution to post SMS messages to the Ozeki NG SMS Gateway through the network. To use this solution first you need to install and configure Ozeki NG SMS Gateway in one of your computers, then you can use the example bellow to send SMS messages through it from any C++ application. This example posts SMS messages through HTTP to the gateway. The Ozeki gateway can convert these posts and forward your SMS messages through SMPP, CIMD2, UCP to SMS service providers or it can operate GSM modems to send your SMS messages using AT commands (Figure 1).
Source code for C++ SMS sending through HTTP
#include<iostream> #include<sstream> #include<windows.h> #include<wininet.h> using namespace std; string encode(string url); int main(int argc, char** argv) { // Ozeki NG SMS Gateway's host // and port. string host = "localhost"; int port = 9502; // Username // and password. string username = "admin"; string password = "abc123"; // Message string message = "Test message!"; // Originator's phone number. string originator = "+001111111"; // Recipient(s) phone number. // If You want to send message to multiple recipients, separate them with coma. // (don't use space character!) Ex: "+002222222,+003333333,+004444444" string recipient = "+002222222"; // 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. HINTERNET inet = InternetOpen("Ozeki", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); // 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 statuscode 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; } system("pause"); } // 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()[i]; if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ){ s << c; } else { if(c == ' ') s << "%20"; else s << '%' << (hex[c >> 4]) << (hex[c & 15]); } } return s.str(); }
More information