linux下编译安装libcurl,使用libcurl编程

您所在的位置:网站首页 libcurl编译安装 linux下编译安装libcurl,使用libcurl编程

linux下编译安装libcurl,使用libcurl编程

#linux下编译安装libcurl,使用libcurl编程| 来源: 网络整理| 查看: 265

​ linux下编译安装libcurl 1.下载libcurl源码: git clone https://github.com/curl/curl.git 2.进入curl工程目录执行 ./buidconf 产生conå gure配置文件: 在这里插入图片描述

3.执行产生的conègure脚本: ./conègure 在这里插入图片描述

4.make 5.make install 默认库文件安装在/usr/local/lib 头文件安装 在/usr/local/include —>安装要root权限 6.到/usr/local/lib/即可查看到安装好的库文件: 在这里插入图片描述

post例子 test.c注意:编译的时候后面要加-lcurl

```c #include #include #include #include #deè ne POSTURL "http://172.24.111.99/webs/platformGB28181" #deè ne POSTFIELDS "server_ip=192.168.110.110&server_port=15060" int main(int argc,char *argv[]){ CURL *curl; CURLcode res; struct curl_slist *http_header = NULL; //初始化curl curl = curl_easy_init(); if (!curl){ fprintf(stderr,"curl init failed\n"); return -1; } curl_easy_setopt(curl, CURLOPT_POST, 1); // post req curl_easy_setopt(curl, CURLOPT_URL, POSTURL); // url curl_easy_setopt(curl, CURLOPT_POSTFIELDS,POSTFIELDS);//post data //设置请求头 http_header = curl_slist_append(http_header, "Content- Type:application/x-www-form-urlencoded"); // http_header = curl_slist_append(http_header, "Content- Type:application/json;charset=UTF-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_header); //headers = curl_slist_append(headers, "Content- Type:application/json;charset=UTF-8"); //headers = curl_slist_append(headers, "Content-Type:application/x- www-form-urlencoded"); curl_easy_setopt(curl, CURLOPT_COOKIE, "LANGSET=1; SaveFlag=0; loginUserName=admin; dataPort=3321; streamPort=7554; loginPassword=admin; usr_session=admin; setOuttimes=1647853392383;"); res = curl_easy_perform(curl); if (http_header != NULL) { curl_slist_free_all(http_header); //free the list again } if (res != CURLE_OK){ switch(res) { case CURLE_UNSUPPORTED_PROTOCOL: fprintf(stderr,"不支持的协议,由URL的头部指定\n"); case CURLE_COULDNT_CONNECT: fprintf(stderr,"不能连接到remote主机或者代理\n"); case CURLE_HTTP_RETURNED_ERROR: fprintf(stderr,"http返回错误\n"); case CURLE_READ_ERROR: fprintf(stderr,"读本地文件错误\n"); default: fprintf(stderr,"返回值:%d\n",res); } return -1; } curl_easy_cleanup(curl); } 编译 gcc test.c -o test -lcurl 封装函数例子 头文件 #include #include #include #include int post_data_to_edge(char *POSTURL,char *POSTFIELDS) { if((POSTURL == NULL)||(POSTFIELDS == NULL)) { return; } CURL *curl; CURLcode res; struct curl_slist *http_header = NULL; //初始化curl curl = curl_easy_init(); if (!curl){ fprintf(stderr,"curl init failed\n"); return -1; }curl_easy_setopt(curl, CURLOPT_POST, 1); // post req curl_easy_setopt(curl, CURLOPT_URL, POSTURL); // url curl_easy_setopt(curl, CURLOPT_POSTFIELDS,POSTFIELDS);//post data //设置请求头 http_header = curl_slist_append(http_header, "Content- Type:application/x-www-form-urlencoded"); //http_header = curl_slist_append(http_header, "Content- Type:application/json;charset=UTF-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_header); //headers = curl_slist_append(headers, "Content- Type:application/json;charset=UTF-8"); //headers = curl_slist_append(headers, "Content-Type:application/x-www- form-urlencoded"); //curl_easy_setopt(curl, CURLOPT_COOKIE, "LANGSET=1; SaveFlag=0; loginUserName=admin; dataPort=3321; streamPort=7554; loginPassword=admin; usr_session=admin; setOuttimes=1647853392383;"); res = curl_easy_perform(curl); if (http_header != NULL) { curl_slist_free_all(http_header); //free the list again } if (res != CURLE_OK) { switch(res) { case CURLE_UNSUPPORTED_PROTOCOL: fprintf(stderr,"不支持的协议,由URL的头部指定\n"); case CURLE_COULDNT_CONNECT: fprintf(stderr,"不能连接到remote主机或者代理\n"); case CURLE_HTTP_RETURNED_ERROR: fprintf(stderr,"http返回错误\n"); case CURLE_READ_ERROR: fprintf(stderr,"读本地文件错误\n"); default: fprintf(stderr,"返回值:%d\n",res); } return -1; } curl_easy_cleanup(curl); return 0; } ​


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3