使用的版本curlcurl-7.69.1
使用的easy接口
1、二進制方式POST圖片
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "http://xxx/res_upload?company=dc&device_name=hg-0677R");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
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, "/D:/system/Desktop/1.png"); /* 本地文件路徑 */
part = curl_mime_addpart(mime);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
2、GET下載
CURL *m_EasyCurlHandle;
CURLcode res;
curl_easy_cleanup(m_EasyCurlHandle);
m_EasyCurlHandle = curl_easy_init();
if(m_EasyCurlHandle)
{
if(file_path)
m_wfd = fopen(file_path, "wb+");
if(!m_wfd)
{
return false;
}
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
//char Auth_header[256]={0};
//snprintf(Auth_header, 256, "Authorization:Bearer %s", m_Tocken);
// headers = curl_slist_append(headers, Auth_header);
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_HTTPHEADER, headers);
//curl_easy_setopt(m_EasyCurlHandle, CURLOPT_HTTPGET, 1L); /* 發送HTTP GET請求 */
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_URL, url); /* URL地址設置 */
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_WRITEFUNCTION, ReceiveDataCB);
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_WRITEDATA, m_wfd); /* 設置自定義指針 */
curl_easy_setopt(m_EasyCurlHandle, CURLOPT_VERBOSE, 1);
CURLcode res = curl_easy_perform(m_EasyCurlHandle);
if(res != CURLE_OK)
{
LOG_DEBUG << "curl_easy_perform() failed: " << curl_easy_strerror(res);
}
curl_slist_free_all(headers);
if(m_wfd)
{
fclose(m_wfd);
m_wfd = NULL;
}
curl_easy_cleanup(m_EasyCurlHandle);
}