#include #include #include #include #include #include #include #include // Callback function to handle response data static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) { size_t total_size = size * nmemb; output->append((char*)contents, total_size); return total_size; } // Function to generate MD5 hash std::string generateMD5(const std::string& input) { unsigned char digest[MD5_DIGEST_LENGTH]; MD5((unsigned char*)input.c_str(), input.length(), digest); char mdString[33]; for(int i = 0; i < 16; i++) sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]); return std::string(mdString); } // Function to build query string from map std::string buildQueryString(const std::map& params) { std::string query; for (const auto& pair : params) { if (!query.empty()) query += "&"; query += pair.first + "=" + pair.second; } return query; } int main() { std::string method = "GET"; // Request method: GET or POST std::string secretType = "MD5"; // Verification method: MD5 or HASH std::string api_url = "https://登录后显示/api/22/22"; std::string appid = "应用id"; // Get from your application settings std::string secret = "应用密钥"; // Get from your application settings // Prepare request data std::map data = { {"appid", "1"}, {"format", "json"}, {"time", "1545829466"} }; data["appid"] = appid; data["time"] = std::to_string(time(nullptr)); // Current server time // Generate signature based on secret type if (secretType == "MD5") { // Sort data by key std::vector> sortedData(data.begin(), data.end()); std::sort(sortedData.begin(), sortedData.end()); // Build MD5 string std::string md5String; for (const auto& pair : sortedData) { if (!pair.second.empty()) { md5String += pair.first + pair.second; } } // Generate MD5 hash data["sign"] = generateMD5(md5String + secret); } else if (secretType == "HASH") { data["sign"] = secret; } // Make the API request CURL* curl = curl_easy_init(); std::string response; if (curl) { if (method == "GET") { std::string url = api_url + "?" + buildQueryString(data); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); } else { // POST request curl_easy_setopt(curl, CURLOPT_URL, api_url.c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded;charset=utf-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); std::string postData = buildQueryString(data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.68.0"); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } // Output the response (in a real application, you'd parse the JSON) std::cout << "API Response:\n" << response << std::endl; return 0; }