curl 怎么在xp下使用_Http Post 快速使用
點擊上方藍字可直接關注!方便下次閱讀。如果對你有幫助,麻煩點個在看或點個贊,感謝~
一直對http很陌生,這次借助libcurl分享一個快速使用http post的案例。
平臺:ubuntu16.04
一、libcurl的安裝
1.?Git上下載 master最新代碼
https://github.com/curl/curl.git
我是下載的Zip包,git clone有點慢
2.?編譯只有configure.ac文件和Makefile.am文件的工程
源碼包還可以用cmake編,但是我失敗了;所以用的傳統./configure方式。
張宇說,沒有條件創造條件,所以構造configure文件。
libtoolizeaclocalautoheaderautoconfautomake --add-missing這個流程親測可用!可以寫個腳本文件,以后直接用就行。注意缺什么安什么就行了。
3.?配置configure參數
./configure --prefix=/opt/libcurl --without-ssl
不使用ssl;設置安裝路徑為/opt/libcurl,方便以后移除。
4.?傳統技能
make
sudo make install
二、使用Python搭建http server
Libcurl是有例子的,在/curl-master/docs/examples下。顯然我沒有仔細看,直接在網上搜別人怎么用的,然后沒用明白,悲傷。
沒有一個server,太難測試了,而搭建server又太難,恰好python解決了這個棘手的問題。只需6行就可以完美解決。
from bottle import route, request, run @route('/hello', method=['GET', 'POST']) def dh(): return 'hello ' + request.query.str if __name__ == "__main__": run(host='0.0.0.0', port=8080, reloader=True)需要安裝python bottle 才可以運行,步驟如下:
sudo apt install python-pip
python2 -m pip install bottle
注:使用的是python2測試的?
運行效果如下:
三、libcurl Post例子
libcurl 的Post功能只是它眾多功能中的一個,其他的我用不到,就不介紹了。
使用cmake構建的工程,主測試程序如下:
#include #include #include #include #include "curl.h"int httpPost( uint8_t * strPost, uint32_t msg_size){ FILE *fpBody = NULL; FILE *fpHeadData = NULL; if ((fpBody = fopen("BodyData", "w")) == NULL) // 返回body用文件存儲 return -1; if ((fpHeadData = fopen("HeaderData", "w")) == NULL) // 返回head用文件存儲 return -1; CURLcode ret; struct curl_slist *headers=NULL; CURL* curl = curl_easy_init(); if(NULL == curl) { return CURLE_FAILED_INIT; } curl_easy_setopt(curl, CURLOPT_POST, 1); //設置為post方式//設置內容類型,可以設置為json,本次測試未使用 // headers = curl_slist_append(headers, "Content-Type: application/octet-stream"); curl_easy_setopt(curl, CURLOPT_URL,"http://127.0.0.1:8080/hello?str=world");; //設置URL //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 改協議頭 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost); //設置post buf curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, msg_size); //設置buf大小,上次就是被坑在這里了 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fpBody); //將返回的bosy數據輸出到fp指向的文件 curl_easy_setopt(curl, CURLOPT_HEADERDATA, fpHeadData); // 將返回的http頭輸出到fp指向的文件 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); //超時時間為3秒 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); ret = curl_easy_perform(curl); //執行 curl_easy_cleanup(curl); //釋放資源 curl_slist_free_all(headers); //釋放資源 if(ret != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(ret)); } return ret; }int main(){ uint8_t postData[] = "postContent"; // 未使用 httpPost( postData, strlen(postData)); return 0;}說下流程:
現在我也不是很懂這些流程,按照相應的格式設置,能和對方server通信即可。
Server說明如下:
①請求類型Http Post
②Http Content-Type: application/octet-stream
效果如下:
四、總結
如何快速使用別人的庫。
生活挺像數學的:已知條件就是你的當下,要你解的卻是未來的你。
你說誰能知道未來的自己會是什么樣?但是有人數學分就是比你高。
由現在是可以大概知道未來的自己會是什么樣的~ ~ ~
總結
以上是生活随笔為你收集整理的curl 怎么在xp下使用_Http Post 快速使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaBean为什么要实现Serial
- 下一篇: C语言--三次方程数值求解