1. #include <stdio.h>
  2. #include <curl/curl.h>
  3.  
  4. bool getUrl(char *filename)
  5. {
  6. CURL *curl;
  7. CURLcode res;
  8. FILE *fp;
  9. if ((fp = fopen(filename, "w")) == NULL) // 返回结果用文件存储
  10. return false;
  11. struct curl_slist *headers = NULL;
  12. // headers = curl_slist_append(headers, "Accept: Agent-007");
  13. curl = curl_easy_init(); // 初始化
  14. if (curl)
  15. {
  16. //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
  17. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
  18. curl_easy_setopt(curl, CURLOPT_URL,"http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC");
  19. // curl_easy_setopt(curl, CURLOPT_URL,"http://localhost/");
  20. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的html主体数据输出到fp指向的文件
  21. curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
  22. res = curl_easy_perform(curl); // 执行
  23. if (res != ) {
  24.  
  25. curl_slist_free_all(headers);
  26. curl_easy_cleanup(curl);
  27. }
  28. fclose(fp);
  29. return true;
  30. }
  31. }
  32. bool postUrl(char *filename)
  33. {
  34. CURL *curl;
  35. CURLcode res;
  36. FILE *fp;
  37.  
  38. struct curl_httppost *formpost = NULL;
  39. struct curl_httppost *lastptr = NULL;
  40.  
  41. if ((fp = fopen(filename, "w")) == NULL)
  42. return false;
  43. curl = curl_easy_init();
  44. if (curl)
  45. {
  46. // curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt"); // 指定cookie文件
  47.  
  48. curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "image",CURLFORM_COPYCONTENTS,"1.jpg",CURLFORM_END);
  49. // curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost); // POST -liuzhenbo
  50. //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post内容
  51. //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
  52. curl_easy_setopt(curl, CURLOPT_URL, "http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC"); // 指定url
  53. curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost); // POST -liuzhenbo
  54. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //HTTP主体数据 -liuzhenbo
  55. curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
  56. res = curl_easy_perform(curl);
  57. curl_easy_cleanup(curl);
  58. curl_formfree(formpost);
  59. }
  60. fclose(fp);
  61. return true;
  62. }
  63. int main(void)
  64. {
  65. getUrl("get");
  66. postUrl("post");
  67. }

GET方式接收到服务器端发来的http头:

  1. HTTP/1.1
  2. Server: nginx/1.10. (Ubuntu)
  3. Date: Mon, Jun :: GMT
  4. Content-Type: application/json;charset=UTF-
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. X-Application-Context: cloud-gateway:

POST方式接收到服务器发来的http头:

  1. HTTP/1.1 Continue
  2.  
  3. HTTP/1.1
  4. Server: nginx/1.10. (Ubuntu)
  5. Date: Mon, Jun :: GMT
  6. Content-Type: application/json;charset=UTF-
  7. Transfer-Encoding: chunked
  8. Connection: keep-alive
  9. X-Application-Context: cloud-gateway:

注:使用HTTP/1.1协议的curl,当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步。(我认为主要是为了节省资源)

 流程如下:

(1)发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据

(2)接收到Server返回的100-continue应答以后, 把数据POST给Server

基于libcurl的GET与POST(HTTP1.1)的更多相关文章

  1. 基于libcurl实现REST风格http/https的get和post

    c/c++开发中经常要用到http/https协议,直接使用socket工作量很大,要是使用socket实现https,那更不可思议,开源的c/c++的http客户端框架,libcurl是首选,而且也 ...

  2. 基于libcurl的restfull接口 post posts get gets

    头文件 #pragma once #ifndef __HTTP_CURL_H__ #define __HTTP_CURL_H__ #include <string> #include &q ...

  3. 基于libcurl的POST(http)

    #include <stdio.h> #include <curl/curl.h> int main (void) { char *url="http://www.n ...

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

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

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

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

  6. libcurl

    一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.libcurl使用 ...

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

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

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

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

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

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

随机推荐

  1. VS2013+HALCON13

    HALCON安装与配置(VS2013+HALCON13) 2017-06-23 16:08:25 坚强的羊脂球 阅读数 4574更多 分类专栏: HALCON   配置主要分为三部分: 1)VS调用H ...

  2. 关于连接查询主要是左右连接查询中,where和on的区别

    工作中,今天用到左连接查询,我自己造的数据,需要根据条件进行筛选,但是筛选不符合我的要求,最终发现是左右连接中where和on的区别,在作怪,工作中用的表关联太多,我下面简化要点,仅仅把注意点写个简单 ...

  3. 配置多个数据源datasource

    https://www.jianshu.com/p/34730e595a8c @Primary.@Qualifire.@Autowired.@Resource https://blog.csdn.ne ...

  4. 四十三、在SAP中初始化勾选值

    一.上代码 二.运行时,勾选框会被自动勾选中 三.表单如下

  5. 027-PHP编码和解码函数base64

    <?php $data = "我爱PHP";//解码前的值 print("我爱PHP: " . base64_encode($data)); //进行解码 ...

  6. 081-PHP的do-while循环

    <?php $i = 1; do { echo $i; $i = $i +1; } while ($i <= 8); ?>

  7. ACM-Fire Net

    题目描述:Fire Net   Suppose that we have a square city with straight streets. A map of a city is a squar ...

  8. RF:connecting to multiple databases

    Hello, I am trying to connect to multiple databases with DatabaseLibrary but its not working. *** Se ...

  9. SpringBoot的Banner横幅

    SpringBoot的Banner横幅即在SpringBoot应用程序启动过程中,日志输出的如下内容: 如果想替换此部分内容的话,可以在classpath根路径下建立一个文件,命名为:banner.t ...

  10. css实现下箭头

    css实现下箭头 .top { width:; height:; border-left: 10px solid transparent; border-right: 10px solid trans ...