90 lines
2.3 KiB
C++
90 lines
2.3 KiB
C++
#include <curl/curl.h>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
// 回调函数,用于接收API响应
|
|
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s)
|
|
{
|
|
size_t newLength = size * nmemb;
|
|
try {
|
|
s->append((char*)contents, newLength);
|
|
} catch (std::bad_alloc& e) {
|
|
// 处理内存不足的情况
|
|
return 0;
|
|
}
|
|
return newLength;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
SetConsoleCP(CP_UTF8);
|
|
#endif
|
|
|
|
// DeepSeek API endpoint
|
|
std::string url = "https://api.deepseek.com/v1/extract";
|
|
|
|
// 你的API密钥
|
|
std::string api_key = "your_api_key_here";
|
|
|
|
// 要上传的文件路径
|
|
std::string file_path = "path/to/your/project.zip";
|
|
|
|
// 初始化libcurl
|
|
CURL* curl;
|
|
CURLcode res;
|
|
std::string response_string;
|
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
curl = curl_easy_init();
|
|
|
|
if (curl) {
|
|
// 设置API URL
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
// 设置HTTP头
|
|
struct curl_slist* headers = nullptr;
|
|
headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key).c_str());
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
// 设置POST请求
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
|
|
// 设置文件上传
|
|
curl_mime* mime;
|
|
curl_mimepart* part;
|
|
mime = curl_mime_init(curl);
|
|
part = curl_mime_addpart(mime);
|
|
curl_mime_name(part, "file");
|
|
curl_mime_filedata(part, file_path.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
|
|
|
|
// 设置回调函数以接收响应
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
|
|
|
|
// 执行请求
|
|
res = curl_easy_perform(curl);
|
|
|
|
// 检查请求是否成功
|
|
if (res != CURLE_OK) {
|
|
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
|
} else {
|
|
std::cout << "API Response: " << response_string << std::endl;
|
|
}
|
|
|
|
// 清理
|
|
curl_mime_free(mime);
|
|
curl_easy_cleanup(curl);
|
|
curl_slist_free_all(headers);
|
|
}
|
|
|
|
curl_global_cleanup();
|
|
return 0;
|
|
} |