下面
srand(time(0));//初始化的時候使用
static char dec2hex(short int c)
{
if (0 <= c && c <= 9)
{
return c + '0';
}
else if (10 <= c && c <= 15)
{
return c + 'A' - 10;
}
else
{
return -1;
}
}
static char* change(char* str)
{
// assert(str != NULL);//斷言,判斷str知否指向空
char* pstr = str;//由于str要進行變化,以後還要用到,是以先把他用pstr存起來
int space_count = 0;//計數器
char* end = NULL;
char* end_new = NULL;
while(*str++!='\0')
{
if(*str == '+' || *str == '*')
space_count++;//進行空格計數
else if(*str == '~')
space_count--;
}
end = str;
end_new = end + 2*space_count;
str = pstr;
while(end != end_new)//當新結束指針和原結束指針不相等時
{
if(*end == '+')
{
*end_new-- = '0';
*end_new-- = '2';
*end_new-- = '%';
end--;
}
else if(*end == '*')
{
*end_new-- = 'A';
*end_new-- = '2';
*end_new-- = '%';
end--;
}
else if(*end == 'E' && *end - 1 == '7' && *end - 2 == '%')
{
*end_new-- = '~';
end -= 3;
}
else//否則進行指派
{
*end_new-- = *end--;
}
}
return pstr;//将變化後的字元串的首位址傳回
}
static void urlencode_v30(char url[])
{
int i = 0;
int len = strlen(url);
int res_len = 0;
char res[1024*5];
for (i = 0; i < len; ++i)
{
char c = url[i];
if ( ('0' <= c && c <= '9') ||
('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
c == '/' || c == '.' || c == '-' || c == '_')
{
res[res_len++] = c;
}
else
{
int j = (short int)c;
if (j < 0)
j += 256;
int i1, i0;
i1 = j / 16;
i0 = j - i1 * 16;
res[res_len++] = '%';
res[res_len++] = dec2hex(i1);
res[res_len++] = dec2hex(i0);
}
}
res[res_len] = '\0';
// printf("%s,Line=%d,res=%s,res_len=%d\n", __FUNCTION__,__LINE__,res,res_len);
strcpy(url, res);
}
static char* SpecialUrlEncode(const char *pSrc)
{
char pTempSrc[1024*5];
memset(pTempSrc,0,sizeof(pTempSrc));
strcpy(pTempSrc,pSrc);
printf("%s,Line=%d,pTempSrc=%s\n", __FUNCTION__,__LINE__,pTempSrc);
urlencode_v30(pTempSrc);
char *pChange = change(pTempSrc);
return pChange;
}
int HtRestSDKDll_ALIPush(const char *pAccessKey,const char *pAccessKeySecret,const char *pAppKey,const char *pTarget,const char *pTitle,const char *pBody)
{
m_nReqID = rand();
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time); //擷取相對于1970到現在的秒數
struct tm nowTime;
gmtime_r(&time.tv_sec, &nowTime);
char pTime[64],pUrlEncode_Time[256];
char pUrlEncode_Title[1024];
char pUrlEncode_Body[1024];
memset(pTime,0,sizeof(pTime));
memset(pUrlEncode_Time,0,sizeof(pUrlEncode_Time));
memset(pUrlEncode_Title,0,sizeof(pUrlEncode_Title));
memset(pUrlEncode_Body,0,sizeof(pUrlEncode_Body));
sprintf(pTime,"%04d-%02d-%02dT%02d:%02d:%02dZ",nowTime.tm_year + 1900,nowTime.tm_mon+1,nowTime.tm_mday,nowTime.tm_hour,nowTime.tm_min,nowTime.tm_sec);
strcpy(pUrlEncode_Time,pTime);
strcpy(pUrlEncode_Title,pTitle);
strcpy(pUrlEncode_Body,pBody);
urlencode_v30(pUrlEncode_Time);
urlencode_v30(pUrlEncode_Title);
urlencode_v30(pUrlEncode_Body);
printf("%s,Line=%d,pUrlEncode_Time=%s,pUrlEncode_Title=%s,pUrlEncode_Body=%s\n", __FUNCTION__,__LINE__,pUrlEncode_Time,pUrlEncode_Title,pUrlEncode_Body);
char pSign[1024*5];
sprintf(pSign,"AccessKeyId=%s&Action=Push&AppKey=%s&Body=%s&DeviceType=ALL&Format=XML&PushType=NOTICE&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=%d&SignatureVersion=1.0&Target=TAG&TargetValue=%s&Timestamp=%s&Title=%s&Version=2016-08-01",
pAccessKey,pAppKey,pUrlEncode_Body,m_nReqID,pTarget,
pUrlEncode_Time,
pUrlEncode_Title);
sprintf(pSign,"GET&%s&%s","%2F",SpecialUrlEncode(pSign));
unsigned char digest[64] = {'\0'};
unsigned int digest_len = 0;
char pTempSecret[128];
memset(pTempSecret,0,sizeof(pTempSecret));
sprintf(pTempSecret,"%s&",pAccessKeySecret);
HMAC(EVP_sha1(), pTempSecret, strlen(pTempSecret), (unsigned char*)pSign, strlen(pSign), digest, &digest_len);
std::string pBase64Rlt = base64_encode(digest, digest_len);
sprintf(pSign,"http://cloudpush.aliyuncs.com/?Signature=%s&AccessKeyId=%s&Action=Push&AppKey=%s&Body=%s&DeviceType=ALL&Format=XML&PushType=NOTICE&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=%d&SignatureVersion=1.0&Target=TAG&TargetValue=%s&Timestamp=%s&Title=%s&Version=2016-08-01",
SpecialUrlEncode(pBase64Rlt.c_str()),pAccessKey,pAppKey,pUrlEncode_Body,m_nReqID,pTarget,
pUrlEncode_Time,
pUrlEncode_Title);
printf("%s,Line=%d,Sign=%s\n", __FUNCTION__,__LINE__,pSign);
http_client_config config;
config.set_timeout(utility::seconds(90)); //設定為90秒逾時
http_client client(pSign, config);
http_request request(methods::GET);
request.headers().add(U("Content-Type"), U("application/json"));
client.request(request).get();
return 0;
}