Files
DNF_DEV/source/Tool/Tool_Network.cpp
2025-10-06 04:18:49 +08:00

83 lines
2.0 KiB
C++

#include "Tool/Tool_Network.h"
// // 定义内存管理结构体
// struct MemoryStruct
// {
// char *memory;
// size_t size;
// };
// size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp)
// {
// size_t realsize = size * nmemb;
// struct MemoryStruct *mem = (struct MemoryStruct *)userp;
// void *ptr = realloc(mem->memory, mem->size + realsize + 1);
// if (!ptr)
// {
// return 0;
// }
// mem->memory = (char *)ptr;
// memcpy(&(mem->memory[mem->size]), contents, realsize);
// mem->size += realsize;
// mem->memory[mem->size] = '\0';
// return realsize;
// }
// int http_get(const char *url, char **response, size_t *response_size)
// {
// CURL *curl;
// CURLcode res;
// struct MemoryStruct chunk;
// // 初始化内存结构
// chunk.memory = (char *)malloc(1);
// chunk.size = 0;
// if (!chunk.memory)
// {
// return -1;
// }
// curl = curl_easy_init();
// if (curl)
// {
// curl_easy_setopt(curl, CURLOPT_URL, url);
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
// // 允许重定向
// curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
// // 执行请求
// res = curl_easy_perform(curl);
// // 检查HTTP状态码
// long response_code;
// curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
// if (res != CURLE_OK)
// {
// free(chunk.memory);
// *response = NULL;
// *response_size = 0;
// curl_easy_cleanup(curl);
// return -1;
// }
// // 返回结果
// *response = chunk.memory;
// *response_size = chunk.size;
// curl_easy_cleanup(curl);
// return 0;
// }
// free(chunk.memory);
// *response = NULL;
// *response_size = 0;
// return -1;
// }