阿里云平台短信发送程序之C++版本:
#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <ctime>
using namespace std;
string UrlEncode(const string& szToEncode);
string sgin(char * key, const char * data);
string replaceall(string str,string old_value,string new_value);
string base64Encode(const unsigned char* Data,int DataByte);
bool SendHttpUrl(string strUrl);
int main(void)
{
char AccessKeyId[33] = "LTAI9u1KJr9mxxxx";//阿里短信秘钥id
char Action[33] = "SendSms";
char SignatureMethod[33] = "HMAC-SHA1";
char SignatureVersion[16] = "1.0";
char SignatureNonce[16] = {0};
char Signature[64] = {0};
char Version[16] = "2017-05-25";
char RegionId[16] = "cn-hangzhou";
char PhoneNumbers[16] = "134xxxxxxxx";需要发送的手机号码
char SignName[32] = "阿里云短信测试专用";
char TemplateCode[16] = "SMS_121085196";//短信模板
char TemplateParam[32] = "{\"code\":\"362387\"}";
char Timestamp[30] = {0};
char Url[512];
char TmpUrl[512];
char strUrl[512];
char accessSecret[33] = "nSPFW86LjkX8jH5zG0ME6Mmpxxxxxx&";//阿里短信密钥
time_t rawTime;
struct tm* timeInfo;
time(&rawTime);
timeInfo = gmtime(&rawTime);
strftime(Timestamp,sizeof(Timestamp),"%Y-%m-%dT%H:%M:%SZ",timeInfo);
snprintf(SignatureNonce,16 - 1 ,"%ld",time(NULL));
snprintf(Url,512 - 1 ,"%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",\
UrlEncode(string("AccessKeyId")).c_str(),UrlEncode(string(AccessKeyId)).c_str(),UrlEncode(string("Action")).c_str(),UrlEncode(string(Action)).c_str(),UrlEncode(string("PhoneNumbers")).c_str(),\
UrlEncode(string(PhoneNumbers)).c_str(),UrlEncode(string("RegionId")).c_str(),UrlEncode(string(RegionId)).c_str(),UrlEncode(string("SignName")).c_str(),UrlEncode(string(SignName)).c_str(),\
UrlEncode(string("SignatureMethod")).c_str(),UrlEncode(string(SignatureMethod)).c_str(),UrlEncode(string("SignatureNonce")).c_str(),UrlEncode(string(SignatureNonce)).c_str(),UrlEncode(string("SignatureVersion")).c_str(),\
UrlEncode(string(SignatureVersion)).c_str(),UrlEncode(string("TemplateCode")).c_str(),UrlEncode(string(TemplateCode)).c_str(),UrlEncode(string("TemplateParam")).c_str(),UrlEncode(string(TemplateParam)).c_str(),UrlEncode(string("Timestamp")).c_str(),UrlEncode(string(Timestamp)).c_str(),\
UrlEncode(string("Version")).c_str(),UrlEncode(string(Version)).c_str());
snprintf(strUrl,512 - 1,"GET&%s&%s",UrlEncode("/").c_str(),UrlEncode(string(Url)).c_str());
string strUrl1 = string(strUrl);
string strUrl3 = replaceall(replaceall(replaceall(strUrl1,"%7E","~"),"*","%2A"),"+","%20");
string Sgin = sgin(accessSecret,strUrl3.c_str());
string strUrl4 = replaceall(replaceall(replaceall(UrlEncode(Sgin),"%7E","~"),"*","%2A"),"+","%20");
snprintf(TmpUrl,512 - 1,"http://dysmsapi.aliyuncs.com/?Signature=%s&%s",strUrl4.c_str(),Url);
if(SendHttpUrl(string(TmpUrl)))
printf("send Sms success!\n");
else
printf("send Sms failed!\n");
return 0;
}
size_t wirtefunc( void *ptr, size_t size, size_t nmemb, void *stream)
{
return size * nmemb;
}
bool SendHttpUrl(string strUrl)
{
curl_global_init(CURL_GLOBAL_ALL);
bool iRet = false;
FILE *fd;
CURL *curl;
CURLcode res;
curl = curl_easy_init(); // 初始化
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL,strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wirtefunc);
res = curl_easy_perform(curl); // 执行
if (res == 0) {
iRet = true;
}
curl_easy_cleanup(curl);
}
return iRet;
}
string replaceall(string str,string old_value,string new_value)
{
while(true){
string::size_type pos(0);
if((pos = str.find(old_value)) != string::npos)
str.replace(pos,old_value.length(),new_value);
else
break;
}
return str;
}
string UrlEncode(const string& szToEncode)
{
std::string src = szToEncode;
char hex[] = "0123456789ABCDEF";
string dst;
for (size_t i = 0; i < src.size(); ++i)
{
unsigned char cc = src[i];
if ( cc >= 'A' && cc <= 'Z'
|| cc >='a' && cc <= 'z'
|| cc >='0' && cc <= '9'
|| cc == '.'
|| cc == '_'
|| cc == '-'
|| cc == '*')
{
if (cc == ' ')
{
dst += "+";
}
else
dst += cc;
}
else
{
unsigned char c = static_cast<unsigned char>(src[i]);
dst += '%';
dst += hex[c / 16];
dst += hex[c % 16];
}
}
return dst;
}
string sgin(char key[], const char * data)
{
unsigned char digest[EVP_MAX_MD_SIZE + 1] = {'\0'};
unsigned int digest_len = 0;
HMAC(EVP_sha1(), key, strlen(key) + 1, (unsigned char*)data, strlen(data), digest, &digest_len);
char mdString[41] = {'\0'};
for(int i = 0; i < digest_len; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
return base64Encode(digest,digest_len);
}
string base64Encode(const unsigned char* Data,int DataByte)
{
//编码表
const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//返回值
string strEncode;
unsigned char Tmp[4]={0};
int LineLength=0;
for(int i=0;i<(int)(DataByte / 3);i++)
{
Tmp[1] = *Data++;
Tmp[2] = *Data++;
Tmp[3] = *Data++;
strEncode+= EncodeTable[Tmp[1] >> 2];
strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
strEncode+= EncodeTable[Tmp[3] & 0x3F];
if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}
}
//对剩余数据进行编码
int Mod=DataByte % 3;
if(Mod==1)
{
Tmp[1] = *Data++;
strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
strEncode+= "==";
}
else if(Mod==2)
{
Tmp[1] = *Data++;
Tmp[2] = *Data++;
strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];
strEncode+= "=";
}
return strEncode;
}