一、libcurl中的http get使用方法

1. 为什么要使用libcurl

1) 作为http的客户端,可以直接用socket连接服务器,然后对到的数据进行http解析,但要分析协议头,实现代理…这样太麻烦了。

2) libcurl是一个开源的客户端url传输库,支持 FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,支持 Windows,Unix,Linux等平台,简单易用,且库文件占用空间不到200K

2. get和post方式

客户端在http连接时向服务提交数据的方式分为get和post两种

3.Get方式将所要传输的数据附在网址后面,然后一起送达服务器,它的优点是效率比较高;缺点是安全性差、数据不超过1024个字符、必须是7位的ASCII编码;查询时经常用此方法

4.Post通过Http post处理发送数据,它的优点是安全性较强、支持数据量大、支持字符多;缺点是效率相对低;编辑修改时多使用此方法

5.Get使用实例,其关键点就是设置回调函数

  1. <pre code_snippet_id="172026" snippet_file_name="blog_20140126_1_7646331" name="code" class="html"></pre>
  2. <pre></pre>
  3. <pre></pre>
  4. <pre></pre>
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "curl/curl.h"
  5. #define HEAD_OUT "/root/project/test/head.out"
  6. #define BODY_OUT "/root/project/test/body.out"
  7. #define RET_EQUAL 0
  8. #define RET_NOT_EQUAL 1
  9. #define RET_NO_UPGRADE_FILE 2
  10. #define RET_FIND_UPGRADE_FILE 3
  11. #define RET_CHECK_OK 4
  12. #define RET_ERROR 5
  13. #define RET_SD_VERSION_ERROR 6
  14. #define RET_OK 7
  15. #define RET_CUR_VERSION_ERROR 8
  16. #define RET_XOR_ERROR 9
  17. #define RET_UPGRADE_OK 10
  18. #define RET_UPGRADE_ERROR 11
  19. #define RET_ERROR_FILE_NAME 12
  20. #define RET_DELAY_CMD_NOT_RUN 13
  21. #define RET_DEALY_CMD_ERROR 14
  22. #define RET_GET_HASH_OK 15
  23. #define RET_GET_HASH_ERROR 16
  24. #define RET_AREADY_CP_VERSION 17
  25. #define RET_NOT_CP_VERSION 18
  26. #define RET_XOR_VERSION 19
  27. #define RET_OPEN_VERSION_LIST_PATH_FAIL 20
  28. #define OPEN_HEAD_FILE_ERROR 21
  29. #define OPEN_BODY_FILE_ERROR 22
  30. #define RET_GET_VERSION_PATH 23
  31. #define RET_NOT_GET_VERSION_PATH 24
  32. #define TIME_OUT 25
  33. #define DOWNLOAD_OK 26
  34. #define LOG fprintf
  35. #define LOG_NOTICE stderr
  36. size_t write_data(void * ptr, size_t size, size_t nmemb, void* stream)
  37. {
  38. int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  39. return written;
  40. }
  41. int detect_version(char * url)
  42. {
  43. CURL * curl_handle;
  44. FILE * headerfile;
  45. FILE * bodyfile;
  46. static const char * headerfilename = HEAD_OUT;
  47. static const char * bodyfilename = BODY_OUT;
  48. // char buffer[STR_LEN] = {'\0'};
  49. int res = 0;
  50. printf("url=%s\n",url);
  51. curl_global_init(CURL_GLOBAL_ALL);
  52. curl_handle = curl_easy_init();
  53. curl_easy_setopt(curl_handle, CURLOPT_URL, url);
  54. curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  55. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  56. headerfile = fopen(headerfilename, "wb");
  57. if(headerfilename == NULL) {
  58. curl_easy_cleanup(curl_handle);
  59. return OPEN_HEAD_FILE_ERROR;
  60. }
  61. bodyfile = fopen(bodyfilename, "wb");
  62. if(bodyfile == NULL) {
  63. curl_easy_cleanup(curl_handle);
  64. return OPEN_BODY_FILE_ERROR;
  65. }
  66. curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
  67. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
  68. res = curl_easy_perform(curl_handle);
  69. if(res != CURLE_OK)
  70. LOG(LOG_NOTICE, "curl_easy_perform() faild:%s\n",
  71. curl_easy_strerror(res));
  72. fclose(headerfile);
  73. fclose(bodyfile);
  74. curl_easy_cleanup(curl_handle);
  75. return res;
  76. }
  77. int main()
  78. {
  79. detect_version("http://car.9797168.com:823/get_bus_info?mac=00:11:22:33:44:55");
  80. return 0;
  81. }

6. POST使用实例,POST使用比较复杂但是比较常用

1.一般使用方法,可以解决大多数post数据的问题。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <curl/curl.h>
  5. #define POSTURL "http://www.xiami.com/member/login"
  6. #define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
  7. #define FILENAME "curlposttest.log"
  8. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
  9. int main(int argc, char *argv[]) {
  10. CURL *curl;
  11. CURLcode res;
  12. FILE *fptr;
  13. struct curl_slist *http_header = NULL;
  14. if ((fptr = fopen(FILENAME, "w")) == NULL) {
  15. fprintf(stderr, "fopen file error: %s\n", FILENAME);
  16. exit(1);
  17. }
  18. curl = curl_easy_init();
  19. curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
  20. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS);   //设置post属性,使用&来将表单属性连接在一起
  21. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);   //回调函数,可有可无
  22. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);           //回调函数写入数据指针
  23. curl_easy_setopt(curl, CURLOPT_POST, 1);                 //设置libcurl发送的协议
  24. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);              //设置版本
  25. curl_easy_setopt(curl, CURLOPT_HEADER, 1);               //设置http数据头
  26. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);       //设置返回的数据量
  27. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");   //设置cookie,不是必须
  28. res = curl_easy_perform(curl);
  29. curl_easy_cleanup(curl);
  30. }
  31. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
  32. FILE *fptr = (FILE*)userp;
  33. fwrite(buffer, size, nmemb, fptr);
  34. }

2.post高级使用方法,特点简单,但不易于理解,其使用二进制传输的方式,适合于加密数据的传输。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "curl/curl.h"
  5. #include "http_rsa.h"
  6. #define POSTURL "http://10.0.0.13:821/wifibox/"
  7. #define HTTP_UPLOAD_FILD "/root/project/upload/upload.tar.gz"
  8. #define HTTP_POST_TYPE_GPS "gps"
  9. #define HTTP_POST_TYPE_SYS "sys"
  10. #define HTTP_REPORT_TYPE "type"
  11. #define HTTP_REPORT_SIGNATURE "signature"
  12. #define HTTP_REPORT_INDEX "ident"
  13. #define HTTP_REPORT_CONTENT "content"
  14. int report_http_post(char* type, char* index, char* content, char* url)
  15. {
  16. struct curl_httppost *post = NULL;
  17. struct curl_httppost *last = NULL;
  18. CURL *easy_handle = curl_easy_init();
  19. char asr_buff[1024]={0};
  20. int len =0;
  21. // 使用multi-parts form post
  22. curl_easy_setopt(easy_handle, CURLOPT_URL, url);
  23. openssl_rsa_enc(type, strlen(type), asr_buff, &len);
  24. // 文本数据
  25. curl_formadd(&post, &last, CURLFORM_COPYNAME, HTTP_REPORT_TYPE, CURLFORM_COPYCONTENTS, type, CURLFORM_END);
  26. curl_formadd(&post, &last, CURLFORM_COPYNAME, HTTP_REPORT_SIGNATURE, CURLFORM_COPYCONTENTS, asr_buff, CURLFORM_END);
  27. // 文本文件中的数据
  28. //curl_formadd(&post, &last, CURLFORM_COPYNAME, "signature", CURLFORM_FILECONTENT, "/root/project/sys", CURLFORM_END);
  29. curl_formadd(&post, &last, CURLFORM_COPYNAME, HTTP_REPORT_INDEX, CURLFORM_COPYCONTENTS, index, CURLFORM_END);
  30. if(1) //发送buff
  31. {
  32. curl_formadd(&post, &last, CURLFORM_COPYNAME, HTTP_REPORT_CONTENT, CURLFORM_COPYCONTENTS, content, CURLFORM_END);
  33. }
  34. else//发送文件
  35. {
  36. curl_formadd(&post, &last, CURLFORM_COPYNAME, HTTP_REPORT_CONTENT, CURLFORM_FILECONTENT, content, CURLFORM_END);
  37. }
  38. curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST, post);
  39. curl_easy_perform(easy_handle);
  40. curl_formfree(post);
  41. curl_easy_cleanup(easy_handle);
  42. curl_global_cleanup();
  43. return 0;
  44. }
  45. #define MSC_INFO "11:22:33:44:55:66"
  46. int main(int argc, char *argv[]) {
  47. report_http_post("buff", MSC_INFO, "{\"date\": \"2013-12-30 12:20:30\"}", POSTURL);
  48. report_http_post("file", MSC_INFO, HTTP_UPLOAD_FILD, POSTURL);
  49. return 0;
  50. }

这个是curl提供的一个高级使用方法,在post/get数据过程中,使用二进制的方式来进行数据交互,解决加密数据传输是乱码的问题。

使用比较简单

1)新建两个发送数据的链表指针

 struct curl_httppost *post = NULL;
 struct curl_httppost *last = NULL;

2)使用curl_formadd函数将链表指针添加进发送队列中。

 curl_formadd(&post,
&last, CURLFORM_COPYNAME, HTTP_REPORT_TYPE, CURLFORM_COPYCONTENTS, type, CURLFORM_END);

CURLFORM_COPYNAME:表单属性名称属性

HTTP_REPORT_TYPE:post表单属性名

CURLFORM_COPYCONTENTS:表单值属性,注意,根据所设置的值不一样,来决定表单属性值的格式,可以是buff,可以是文件,图片等。

type:post表单属性值


3)当然,使用curl_formadd方式和一般的curl方法是一样的,需要一般的初始化流程

CURL
*easy_handle = curl_easy_init();等

注意,此处可以和一般的curl使用方式兼容,可以设置返回值得回调函数等。

libcurl库的http get和http post使用【转】的更多相关文章

  1. C++ 用libcurl库进行http通讯网络编程

    使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...

  2. C++ 用libcurl库进行http通讯网络编程(转)

    转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...

  3. Cocos2d-x移植到WindowsPhone8移植问题-libcurl库移植问题

    在Cocos2d-x 3.x最新版本中提供了Windows Phone 8平台移植libcurl库所需要的头文件和库文件.但要在Windows Phone 8平台成功移植libcurl库还是很不容易, ...

  4. Cocos开发中Visual Studio下libcurl库开发环境设置

    我们介绍一下win32中Visual Studio下libcurl库开发环境设置.Cocos2d-x引擎其实已经带有为Win32下访问libcurl库,Cocos2d-x 3.x中libcurl库文件 ...

  5. C++ 用libcurl库进行http通讯网络编程[转]

    http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...

  6. C/C++ 用libcurl库进行http通讯网络编程

    C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...

  7. C语言 HTTP上传文件-利用libcurl库上传文件

    原文  http://justwinit.cn/post/7626/ 通常情况下,一般很少使用C语言来直接上传文件,但是遇到使用C语言编程实现文件上传时,该怎么做呢? 借助开源的libcurl库,我们 ...

  8. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  9. HTTP多线程下载+断点续传(libcurl库)

    目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.lib ...

  10. 最全的libcurl库资源整理

    C++ 用libcurl库进行http 网络通讯编程 百度登陆协议分析!!!用libcurl来模拟百度登陆 C++使用libcurl做HttpClient 使用libcurl库进行HTTP的下载 li ...

随机推荐

  1. Web Service(一):初识

    1. 前言 cxf 在项目中应用好久了,一直没有写总结,现在补上. 由于cxf 属于Web Service的一个实现,所以先学习和总结一下Web Service,作为学习cxf的基础. 2. WebS ...

  2. Android-Activity的切换效果

    Android-Activity的切换效果 Android-Activity的切换效果 Activity有一个默认的切换效果,但是有时候单一的切换效果未免单调,Activity的切换效果也是我们可以自 ...

  3. 单源最短路模板 + hdu - 2544

    Floyd Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]. /** * Night gathers, and no ...

  4. How to properly set clock speed for STM32F4xx devices

    http://stm32f4-discovery.com/2015/01/properly-set-clock-speed-stm32f4xx-devices/ I see that you have ...

  5. 谨慎注意WebBrowser控件的DocumentCompleted事件

    引言 WebBrowser控件的DocumentCompleted事件一般就被认定为是在页面完全加载完毕后产生,而注释中也是这么写的: 但事实却并非如此. 首先它不一定会在完全加载完毕时才触发,有时就 ...

  6. MyEclipse使用总结——设置MyEclipse使用的Tomcat服务器

    一.设置使用的Tomcat服务器 如果不想使用MyEclipse自带的tomcat服务器版本,那么可以在MyEclipse中设置我们自己安装好的tomcat服务器 设置步骤如下: Window→Pre ...

  7. 交叉编译Python-2.7.13到ARM(aarch64)平台

    方法跟交叉编译Python-2.7.13到ARM(aarch32)平台基本一样, 不同的地方只是把工具链换成编译aarch64的工具链,这里可以参考用qemu搭建aarch64学习环境. 创建目录: ...

  8. springboot static方法与构造方法加载@VALUE

    application.properties文件 mongodb.host=host111 mongodb.port=port222 import org.springframework.beans. ...

  9. file is universal (4 slices) but does not contain a(n) armv7s slice

    关于ld: file is universal (2 slices) but does not contain a(n) armv7s slice 升级了xcode之后,支持iOS6和iPhone5, ...

  10. Select、Poll与Epoll比較

    (1)select select最早于1983年出如今4.2BSD中,它通过一个select()系统调用来监视多个文件描写叙述符的数组.当select()返回后,该数组中就绪的文件描写叙述符便会被内核 ...