(原创)cpprestsdk实现通过阿里云移动推送

下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
}

多谢打赏
-------------本文结束感谢您的阅读-------------
0%