做一个营销型网站多少钱,wordpress 主题制作 视频教程,小型视频网站建设,化妆品网站建设网站点击上方蓝字可直接关注#xff01;方便下次阅读。如果对你有帮助#xff0c;麻烦点个在看或点个赞#xff0c;感谢~一直对http很陌生#xff0c;这次借助libcurl分享一个快速使用http post的案例。平台#xff1a;ubuntu16.04一、libcurl的安装1. Git上下载 master最新代… 点击上方蓝字可直接关注方便下次阅读。如果对你有帮助麻烦点个在看或点个赞感谢~一直对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. 传统技能makesudo make install二、使用Python搭建http serverLibcurl是有例子的在/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(host0.0.0.0, port8080, reloaderTrue)需要安装python bottle 才可以运行步骤如下sudo apt install python-pippython2 -m pip install bottle注使用的是python2测试的 运行效果如下三、libcurl Post例子libcurl 的Post功能只是它众多功能中的一个其他的我用不到就不介绍了。使用cmake构建的工程主测试程序如下#include #include #include #include #include curl.hint 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 *headersNULL; 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?strworld);; //设置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效果如下四、总结如何快速使用别人的库。生活挺像数学的已知条件就是你的当下要你解的却是未来的你。你说谁能知道未来的自己会是什么样但是有人数学分就是比你高。由现在是可以大概知道未来的自己会是什么样的~ ~ ~