使用libcurl進行基于http get/post模式的C語言交互應用開發
- 簡介
- 環境準備
- 在線資源
- 示例代碼
- 測試調用
- 運行結果
簡介
大多數在linux下的開發者,都會用到curl這個命令行工具。對于進行restful api的測試等,非常方便。其實,這個工具還提供了一個C的開發庫,可以很方便的在C語言開發環境下完成基于http的請求和響應交互,高效的開發基于http/smtp等的網絡應用程序
/* 2023-08-14 更新宏定義 1. 使用可變參數,支持多項輸出; 2. 去除Z中默認加上的雙引號;
*/
#define X_LOG_DEBUG(Z, X...) \printf("[%s %s] [%s.%d] [%s] [DEBUG] " Z "\n", __DATE__, __TIME__, __FILE__, __LINE__, __FUNCTION__, ##X)
環境準備
下載并安裝curl的開發包
yum install libcurl-devel.x86_64
在線資源
開發過程中主要參考CURL官方介紹及API參考文檔 | link
示例代碼
多余的話就不多說了,直接上示例代碼,通過代碼中的注釋來說明開發過程。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../include/xhttp.h"/*
- 這個是一個回調函數,主要用于curl在執行過程中,當有被請求的數據到達時,被調用來向-curl_easy_setopt設置的chunk中寫入數據。這個回調函數在curl_easy_perform執行完成前會被調用多次。當執行完成后,從chunk中取出這次交互返回的數據。
*/
static size_t cb_write_data(void *data, size_t size, size_t nmemb, void *clientp)
{size_t realsize = size * nmemb;http_response *mem = (http_response *)clientp;char *ptr = realloc(mem->response, mem->size + realsize + 1);if(ptr == NULL)return 0; /* out of response_st! */mem->response = ptr;memcpy(&(mem->response[mem->size]), data, realsize);mem->size += realsize;mem->response[mem->size] = 0;return realsize;
}/*這個是向外發布的一個函數,調用的方式示例如下:char* payload = "{\"code\":\"\",\"codeUuid\":\"\",\"loginName\":\"user@domain\",\"loginPwd\":\"xxxxxxxx\"}";http_response *resp = http_post("https://local.domain/admin-api/session/login", NULL, payload);使用完返回數據后,記得釋放resp->reesponse,避免內存漏。
*/
http_response* http_post(char* url, char* token, char* payload)
{http_response chunk = {0};/* 設置curl上下文,對curl實例進行初始化 */curl_global_init(CURL_GLOBAL_ALL);CURL *curl = curl_easy_init();CURLcode res;if(curl) {X_LOG_DEBUG("%s", "libcurl curl_easy_setopt start ...");/* 設置curl各個參數: 請求地址 */curl_easy_setopt(curl, CURLOPT_URL, url);/* 設置curl各個參數: 請求方式為post */curl_easy_setopt(curl, CURLOPT_POST, 1L);/* 設置curl各個參數: http中的請求頭部分的內容 */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_HTTPHEADER start ...");struct curl_slist *list = {0};list = curl_slist_append(NULL, "Content-Type: application/json;charset=utf8");list = curl_slist_append(list, "routeurl: /project/project-list");/* 設置curl各個參數: 可選部分,如果請求中要求token,可以設置上 */if (token != NULL){char* x_access_token_str = (char*)malloc( MAX_UTMP_TOKEN_SIZE );sprintf(x_access_token_str, "x-access-token: %s", token);X_LOG_DEBUG("%s", x_access_token_str);list = curl_slist_append(list, x_access_token_str);}curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_USERAGENT start ...");/* some servers do not like requests that are made without a user-agent field, so we provide one */curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0");/* 設置curl各個參數: http的請求體部分,主要是請求中攜帶的數據 *//* POST data */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_POSTFIELDSIZE/CURLOPT_POSTFIELDS start ...");X_LOG_DEBUG("request body data is:%s", payload);X_LOG_DEBUG("request body len is:%d", strlen(payload));/* size of the POST data */curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(payload));/* pass in a pointer to the data - libcurl will not copy */curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);/* 設置curl各個參數: 重要部分,設置了返回值的回調函數和返回值的內存放置區域 *//* RECEIVE DATA */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA start ...");/* send all data to this function */curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write_data);/* we pass our 'chunk' struct to the callback function */curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);X_LOG_DEBUG("%s", "libcurl curl_easy_setopt successfully complete ...");/* 執行curl請求,并獲取返回值:CURLE_OK,表示執行成功 */X_LOG_DEBUG("%s", "libcurl curl_easy_perform start ...");res = curl_easy_perform(curl);X_LOG_DEBUG("%s", "libcurl curl_easy_perform successfully complete ...");if(res != CURLE_OK) {fprintf(stderr, "curl_easy_perform() is failed: %s", curl_easy_strerror(res));curl_slist_free_all(list); /* free the list again */curl_easy_cleanup(curl);} else {/* 處理curl返回的數據 */X_LOG_DEBUG("chunk size %d", chunk.size);http_response response = {0};response.response = (char*)malloc(1048576*5);memset(response.response, 0, chunk.size+1);memcpy(response.response, chunk.response, chunk.size+1);response.size = chunk.size;/* remember to free the buffer */free(chunk.response);curl_slist_free_all(list); /* free the list again */ curl_global_cleanup();return &response;}}
}
引用的頭文件如下:
#ifndef __X_HTTP_H__
#define __X_HTTP_H__#include <stdlib.h>
#include <curl/curl.h>
/* 更新宏定義 1. 使用可變參數,支持多項輸出; 2. 去除Z中默認加上的雙引號; */
#define X_LOG_DEBUG(Z, X...) \printf("[%s %s] [%s.%d] [%s] [DEBUG] " Z "\n", __DATE__, __TIME__, __FILE__, __LINE__, __FUNCTION__, ##X)#define MAX_UTMP_TOKEN_SIZE 8192typedef struct http_response_s {char *response;size_t size;
} http_response;typedef struct http_request_s {char *request;size_t size;
} http_request;http_response* http_post(char* url, char* token, char* payload);#endif
測試調用
int main(int argc, char** argv) {char* payload = "{\"code\":\"\",\"codeUuid\":\"\",\"loginName\":\"user@domain\",\"loginPwd\":\"xxxxxxxx\"}";http_response *resp = http_post("https://local.domain/admin-api/session/login", NULL, payload);printf("http_response [%d] is: %s\n", resp->size, resp->response);char *tokenVal = strstr(resp->response, "xaccessToken");int end = strlen(tokenVal);*(tokenVal + end-2) = 0;char** token_arr;__strtok_r(tokenVal, ":", token_arr);char* replacementKey = strtrim(token_arr[0], '\"');exit(0)
}
運行結果
[Aug 11 2023 10:20:02] [src/xhttp.c.37] [http_post] [DEBUG] "libcurl curl_easy_setopt start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.42] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_HTTPHEADER start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.55] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_USERAGENT start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.61] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_POSTFIELDSIZE/CURLOPT_POSTFIELDS start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.62] [http_post] [DEBUG] "request body data is:{"code":"","codeUuid":"","loginName":"user@domain","loginPwd":"xxxxxxxx"}"
[Aug 11 2023 10:20:02] [src/xhttp.c.63] [http_post] [DEBUG] "request body len is:75"
[Aug 11 2023 10:20:02] [src/xhttp.c.70] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.76] [http_post] [DEBUG] "libcurl curl_easy_setopt successfully complete ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.79] [http_post] [DEBUG] "libcurl curl_easy_perform start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.81] [http_post] [DEBUG] "libcurl curl_easy_perform successfully complete ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.90] [http_post] [DEBUG] "chunk size 6693"