forked from netease-im/NERecord-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpwrapper.cpp
79 lines (69 loc) · 1.98 KB
/
httpwrapper.cpp
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
#include "httpclient.h"
#include "httpwrapper.h"
#include "./fjson/json_builder/fjson_builder.h"
#include "./fjson/json_parser/fjson_parser.h"
#include "./fjson/json_helper.h"
#include <regex>
#include <stdarg.h>
#include <algorithm>
#include <array>
namespace {
unsigned char ToHex(unsigned char x) {
return x > 9 ? x + 55 : x + 48;
}
}
std::string GetURLEncode(const std::string& origin) {
std::string result;
size_t length = origin.length();
for (size_t i = 0; i < length; i++) {
if (isalnum((unsigned char)origin[i]) || (origin[i] == '-') || (origin[i] == '_') || (origin[i] == '.') ||
(origin[i] == '~')) {
result += origin[i];
} else if (origin[i] == ' ') {
result += "+";
} else {
result += '%';
result += ToHex((unsigned char)origin[i] >> 4);
result += ToHex((unsigned char)origin[i] % 16);
}
}
return result;
}
HttpWrapper::HttpWrapper()
{
}
HttpWrapper::~HttpWrapper()
{
}
std::string HttpWrapper::GetCheckSum(const std::string &appkey, const std::string &cname, uint64_t uid, const std::string& token_server)
{
std::string checksum;
std::string url = token_server + "?appkey=" + appkey + "&channelName=" + GetURLEncode(cname) + "&type=1&uid=" + std::to_string(uid);
CurlHttpPost httpPost(url);
if (!httpPost.Create(2000))
{
return checksum;
}
#ifdef USE_PROXY
httpPost.ConfigProxy(CURLPROXY_SOCKS5, SOCKS5_HOST, SOCKS5_PORT, SOCKS5_USER, SOCKS5_PASS);
#endif
int code;
std::string content;
code = httpPost.DoEasy();
if (code == CURLE_OK)
{
content = httpPost.GetRetContent();
json_value *jv = json_parse(content.c_str(), content.size());
if (nullptr != jv)
{
checksum = json_value_find_as_string(jv, "checksum");
if (checksum.empty())
{
printf("Fauilre to request token: %s\n", content.c_str());
} else {
printf("Request Token: %s\n", checksum.c_str());
}
}
}
return checksum;
}