前言
libevent和libcurl都是功能強大的開源庫;libevent主要實現服務器,包含了select、epoll等高並發的實現;libcurl實現了curl命令的API封裝,主要作為客戶端。這兩個開源庫的安裝可以參考我的這篇博客:https://www.cnblogs.com/liudw-0215/p/9917422.html,並且我的代碼都提交在了我的github上了,可以點左上角圖標,跳轉到github,倉庫是libcurl。
一、curl的兩種使用方法
1、命令行模式
所謂命令行模式,就是直接linux的命令行直接可以執行的curl命令,curl可以做很多事情,我主要介紹作為客戶端發送xml和json數據,因為命令行模式非常要注意格式問題!
(1)發送xml格式數據
格式如下:
echo '<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:itsm="http://itsm.soa.csg.cn/"> <soapenv:Header xmlns:auth="http://itsm.soa.csg.cn/"> <auth:user>local_admin</auth:user> <auth:password>local_admin</auth:password> </soapenv:Header> <soapenv:Body> <itsm:accountOper> <operType>1</operType> <operItems> <operItem> <deviceName>測試虛擬機181106</deviceName> <deviceIP>11.11.22.23</deviceIP> <protocol>設備帳戶</protocol> <accountName>administrator</accountName> </operItem> </operItems> </itsm:accountOper> </soapenv:Body> </soapenv:Envelope> '|curl -X POST -H 'Content-type:text/xml' -d @- http://10.94.1.167:80/ITSMWebServer/itsm
說明:
-
- echo后面跟的是xml格式數據,格式一般都是跟第三方平台約定好的,不能發這種格式,接收又是另一種格式,那沒法解析了,都要提前約定好的!
- 中間是“|”管道符,將echo的輸出作為curl的輸入
- POST 說明是post請求
- -H 攜帶的消息頭
- 最后的url,是要發送的地址
(2)發送json格式數據
格式如下:
curl -H "Content-Type:application/json" -H "appName:spvas" -H "password:123123" -H "pswdHashType:SHA1" -X POST -k -g -d '{"param":[{"objectID":112,"type":1,"operate":1,"operatorID":100,"result":0,"time":1539941168,"policytype":0}]}' http://172.16.1.21:9999/rest/spvas/objChange.do
說明:
- -H 依然是消息頭
- -d 后面是json格式的數據了
2、libcurl庫使用
1、安裝
想要使用libcurl庫,首先需要先安裝,安裝參考我的這篇博客寫的很詳細:https://www.cnblogs.com/liudw-0215/p/9917422.html
2、使用libcurl的API
主要就是調用libcurl庫的API接口,下面介紹的http的POST請求,libcurl很多接口,不能一一介紹,需要時可以再去查找。
(1)初始化curl句柄
CURL* curl = NULL;
curl = curl_easy_init();
(2)設置curl的url
curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.1.96:7777/login");
(3)開啟post請求開關
curl_easy_setopt(curl, CURLOPT_POST, true);
(4)添加post數據
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
(5)設定一個處理服務器響應的回調函數
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);
(6)給回調函數傳遞一個形參
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);
(7)向服務器發送請求,等待服務器的響應
res = curl_easy_perform(curl);
3、總體代碼
客戶端總體代碼如下:

// // Created by ldw on 2018/11/8. // #include "cJSON.h" #include <curl/curl.h> #include<string.h> #define RESPONSE_DATA_LEN 4096 //用來接收服務器一個buffer typedef struct login_response_data { login_response_data() { memset(data, 0, RESPONSE_DATA_LEN); data_len = 0; } char data[RESPONSE_DATA_LEN]; int data_len; }response_data_t; //處理從服務器返回的數據,將數據拷貝到arg中 size_t deal_response(void *ptr, size_t n, size_t m, void *arg) { int count = m*n; response_data_t *response_data = (response_data_t*)arg; memcpy(response_data->data, ptr, count); response_data->data_len = count; return response_data->data_len; } #define POSTDATA "{\"username\":\"gailun\",\"password\":\"123123\",\"driver\":\"yes\"}" int main() { char *post_str = NULL; CURL* curl = NULL; CURLcode res; response_data_t responseData;//專門用來存放從服務器返回的數據 //初始化curl句柄 curl = curl_easy_init(); if(curl == NULL) { return 1; } //封裝一個數據協議 /* ====給服務端的協議==== http://ip:port/login [json_data] { username: "gailun", password: "123123", driver: "yes" } * * * */ //(1)封裝一個json字符串 cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "username", "ldw"); cJSON_AddStringToObject(root, "password", "123123"); cJSON_AddStringToObject(root, "driver", "yes"); post_str = cJSON_Print(root); cJSON_Delete(root); root = NULL; //(2) 向web服務器 發送http請求 其中post數據 json字符串 //1 設置curl url curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.1.96:7777/login"); //客戶端忽略CA證書認證 用於https跳過證書認證 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); //2 開啟post請求開關 curl_easy_setopt(curl, CURLOPT_POST, true); //3 添加post數據 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str); //4 設定一個處理服務器響應的回調函數 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response); //5 給回調函數傳遞一個形參 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData); //6 向服務器發送請求,等待服務器的響應 res = curl_easy_perform(curl); if (res != CURLE_OK) { return 1; } curl_easy_cleanup(curl); //(3) 處理服務器響應的數據 此刻的responseData就是從服務器獲取的數據 /* //成功 { result: "ok", } //失敗 { result: "error", reason: "why...." } * * */ //(4) 解析服務器返回的json字符串 //cJSON *root; root = cJSON_Parse(responseData.data); cJSON *result = cJSON_GetObjectItem(root, "result"); if(result && strcmp(result->valuestring, "ok") == 0) { printf("data:%s\n",responseData.data); //登陸成功 return 0; } else { //登陸失敗 cJSON* reason = cJSON_GetObjectItem(root, "reason"); if (reason) { //已知錯誤 return 1; } else { //未知的錯誤 return 1; } return 1; } return 0; }
這是客戶端的總體代碼,但是還無法測試,因為沒有服務端,下面會介紹用libevent庫來搭建http的服務端;因為數據格式是json,所以用到了cJSON,可以到我的github上進行下載,編譯命令:g++ login.cpp cJSON.cpp -o login -lcurl
二、libevent庫
1、安裝
libevent依然是開源庫,使用之前依然需要安裝,安裝參考我的這篇博客寫的很詳細:https://www.cnblogs.com/liudw-0215/p/9917422.html
2、搭建http服務器
安裝之后,就可以使用了,主要都是調用libcurl庫的API函數,main函數如下:
int main(int argc, char *argv[]) { //自定義信號處理函數 signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); //默認參數 char *httpd_option_listen = "0.0.0.0"; int httpd_option_port = 7777; int httpd_option_daemon = 0; int httpd_option_timeout = 120; //in seconds //獲取參數 int c; while ((c = getopt(argc, argv, "l:p:dt:h")) != -1) { switch (c) { case 'l' : httpd_option_listen = optarg; break; case 'p' : httpd_option_port = atoi(optarg); break; case 'd' : httpd_option_daemon = 1; break; case 't' : httpd_option_timeout = atoi(optarg); break; case 'h' : default : show_help(); exit(EXIT_SUCCESS); } } //判斷是否設置了-d,以daemon運行 if (httpd_option_daemon) { pid_t pid; pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } if (pid > 0) { //生成子進程成功,退出父進程 exit(EXIT_SUCCESS); } } /* 使用libevent創建HTTP Server */ //初始化event API event_init(); //創建一個http server struct evhttp *httpd; httpd = evhttp_start(httpd_option_listen, httpd_option_port); evhttp_set_timeout(httpd, httpd_option_timeout); //也可以為特定的URI指定callback evhttp_set_cb(httpd, "/", httpd_handler, NULL); evhttp_set_cb(httpd, "/login", login_handler, NULL); //循環處理events event_dispatch(); evhttp_free(httpd); return 0; }
3、測試http服務
- 啟動服務端
從我的github上下載之后,http服務在libcurl/http_server/這個目錄,寫Makefile,然后直接make就可以了,如下:
make之后生成了server,執行:./server,啟動服務
- 啟動客戶端
在libcurl/login/這個目錄,執行:g++ login.cpp cJSON.cpp -o login -lcurl,進行編譯,生成login,啟動客戶端:./login,客戶端運行結果,如下:
服務端響應結果,如下:
至此,完成了演示,用libcurl和libevent搭建的http服務器與客戶端,沒有問題。是不是覺得到此就結束了,才沒有呢?下面,將要介紹https服務器,那為什么要用https服務器呢?跟隨我找到謎底吧!
4、搭建https服務器
(1)https介紹
http傳輸過程都是明文傳輸,很不安全;就產生https,進行加密傳輸,但加密過程並沒有那么簡單,如下圖所示:
說明:
主要經歷了兩個階段:
- 非對稱加密過程
通過公鑰、私鑰和CA證書,進行驗證,最終獲得會話密鑰
- 對稱加密過程
可能會想?直接都用非對稱加密得了,為啥用對稱加密?因為非對稱效率很低,所以要用對稱加密!
用非對稱過程得到的密鑰,對數據進行加密然后傳輸。
(2)https服務器實現
libevent庫應該從2.1版本之后才支持https的,所以在2.1之前的版本還要單獨安裝openssl!
mian函數如下:
int main (int argc, char **argv) { /*OpenSSL 初始化 */ common_setup (); if (argc > 1) { char *end_ptr; long lp = strtol(argv[1], &end_ptr, 0); if (*end_ptr) { fprintf(stderr, "Invalid integer\n"); return -1; } if (lp <= 0) { fprintf(stderr, "Port must be positive\n"); return -1; } if (lp >= USHRT_MAX) { fprintf(stderr, "Port must fit 16-bit range\n"); return -1; } serverPort = (unsigned short)lp; } /* now run http server (never returns) */ return serve_some_http (); }
(3)測試https服務器
- 啟動服務端
從我的github上下載之后,http服務在libcurl/https_server/這個目錄,寫Makefile,然后直接make就可以了;
- 啟動客戶端
修改http的客戶端就可以了,如下:
curl_easy_setopt(curl, CURLOPT_URL, "https://172.16.1.96:8080/login"); //客戶端忽略CA證書認證 用於https跳過證書認證 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
說明:
在http后面加上“s”;再加上跳過證書認證,就可以了