C++使用curl庫的完成流程
1.下載
官網:https://curl.se/download.html
找到Windows 64-bit,選擇第一項下載
2.解壓到指定路徑,我這里解壓到D:\Program Files下面
3.創(chuàng)建c++控制臺應用程序,右鍵點擊屬性
再點擊VC++目錄 -> 包含目錄,添加路徑“D:\Program Files\curl-8.9.1_1-win64-mingw\include;$(IncludePath);”
再點擊VC++目錄 -> 庫目錄,添加路徑“D:\Program Files\curl-8.9.1_1-win64-mingw\lib;”
再點擊鏈接器 -> 輸入 -> 附加依賴項,添加“libcurl.dll.a”和“libcurl.a”
4.打開路徑“D:\Program Files\curl-8.9.1_1-win64-mingw\bin”,找到“libcurl-x64.dll”文件,并復制到項目根目錄下的“x64/Debug/”下
5.添加頭文件和curl的請求代碼,如下,點擊運行返回請求結果
#include <iostream> #include <curl/curl.h> char* strToChar(std::string strSend) { char* ConvertData; const int len2 = strSend.length(); ConvertData = new char[len2 + 1]; strcpy(ConvertData, strSend.c_str()); return ConvertData; } int main() { std::string _serverUrl = ""; _serverUrl.append("https://www.baidu.com"); CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, strToChar(_serverUrl)); /* cache the CA cert bundle in memory for a week */ curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); /* Perform the request, res gets the return code */ res = curl_easy_perform(curl); /* Check for errors */ if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); }
到此這篇關于C++使用curl庫的完成流程的文章就介紹到這了,更多相關C++使用curl庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!