一、初步了解curl庫
curl是一個用於傳輸數據的開源庫,支持包括http、ftp、smtp等多種傳輸協議。curl庫的使用可以基於curl命令行工具,也可以採用libcurl庫進行開發。
在使用curl_easy_perform進行網絡數據請求之前,需要先了解curl的基本概念和庫函數。常用的幾個函數包括:
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
CURLcode curl_easy_perform(CURL *handle);
void curl_easy_cleanup(CURL *handle);
其中,curl_easy_setopt函數用於設置curl的選項,包括url、請求頭、請求方法等;curl_easy_perform函數執行網絡請求,並返回結果;curl_easy_cleanup函數釋放CURL對象。
下面是一個最簡單的curl例子:
#include <curl/curl.h>
int main(void){
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
上面的例子訪問了Google的主頁,並通過res變量判斷請求是否成功。
二、使用curl_easy_perform進行GET請求
GET請求是最簡單的網絡請求方式,可以通過curl_easy_setopt函數設置請求的url即可。
#include <curl/curl.h>
#include <stdio.h>
int main(){
CURL *curl;
CURLcode res;
char *data;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
上面的代碼使用了curl_easy_setopt(CURLOPT_URL)函數將需要請求的URL傳遞給CURL,然後執行請求並處理結果,最後使用curl_easy_cleanup函數釋放CURL請求。
三、使用curl_easy_perform進行POST請求
POST請求需要在請求頭中傳遞數據,需要使用curl_easy_setopt函數設置CURLOPT_POST和CURLOPT_POSTFIELDS兩個選項。
#include <curl/curl.h>
#include <string.h>
#include <stdio.h>
int main()
{
CURL *curl;
CURLcode res;
char *data = "name=Bob&age=20";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
上面的代碼使用了curl_easy_setopt(CURLOPT_POSTFIELDS)函數設置請求體,並使用curl_easy_setopt(CURLOPT_POST)函數告知curl發送POST請求。
四、使用curl_easy_perform進行文件下載
文件下載可以使用libcurl庫提供的CURLOPT_WRITEFUNCTION回調函數。該函數需要指定一個可執行指針,用於接收下載的數據並保存到本地文件中。以下是一個簡單的文件下載示例:
#include <curl/curl.h>
#include <stdio.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream){
return fwrite(ptr, size, nmemb, stream);
}
int main(){
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
FILE *fp;
fp = fopen("example.jpg", "wb");
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/example.jpg");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
上面的代碼中,write_data函數即為CURLOPT_WRITEFUNCTION回調函數,將下載數據保存到本地example.jpg文件中。curl_easy_setopt(CURLOPT_WRITEDATA)函數指定回調函數輸出文件。
五、使用curl_easy_perform進行訪問HTTPS
訪問HTTPS需要開啟TLS/SSL支持,主要包括以下幾個步驟:
- 引入openssl庫頭文件
- 開啟CURLOPT_SSL_VERIFYPEER選項
- 設置CURLOPT_CAINFO選項
- 設置CURLOPT_SSLCERT和CURLOPT_SSLKEY選項(可選)
以下是一個訪問HTTPS網站的示例:
#include <curl/curl.h>
#include <stdio.h>
int main(){
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/cert.pem");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
上面的代碼中,使用curl_global_init(CURL_GLOBAL_SSL)函數初始化curl庫,然後開啟CURLOPT_SSL_VERIFYPEER選項。另外,需要設置CURLOPT_CAINFO選項,用於指定證書文件,CURLOPT_SSLCERT和CURLOPT_SSLKEY可選。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/152094.html