C++要实现http网络连接,需要借助第三方库,libcurl使用起来还是很方便的

环境:win32 + vs2015

如果要在Linux下使用,基本同理

1,下载编译libcurl

下载curl源码,找到vs工程,按照x86 x64 并对应debug和release编译出静态库lib

2,构建工程

1)curl头文件和lib拷贝到工程目录

2)配置附加包含目录libcurl中的include和附加库目录libcurl中的lib目录

3)添加预编译宏USE_OPENSSL和CURL_STATICLIB

4)添加如依赖库

crypt32.lib
ws2_32.lib
wldap32.lib
libcurl.lib

注意版本对应

3,代码示例

  1. #include <iostream>
  2. #include <string>
  3. #include "curl/curl.h"
  4. using namespace std;
  5. #pragma comment(lib, "ws2_32.lib")
  6. #pragma comment(lib, "wldap32.lib")
  7. #pragma comment(lib, "libcurl.lib")
  8. // reply of the requery
  9. size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
  10. {
  11. cout << "----->reply" << endl;
  12. string *str = (string*)stream;
  13. cout << *str << endl;
  14. (*str).append((char*)ptr, size*nmemb);
  15. return size * nmemb;
  16. }
  17. // http GET
  18. CURLcode curl_get_req(const std::string &url, std::string &response)
  19. {
  20. // init curl
  21. CURL *curl = curl_easy_init();
  22. // res code
  23. CURLcode res;
  24. if (curl)
  25. {
  26. // set params
  27. curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
  28. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
  29. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
  30. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  31. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  32. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
  33. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
  34. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  35. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  36. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time
  37. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  38. // start req
  39. res = curl_easy_perform(curl);
  40. }
  41. // release curl
  42. curl_easy_cleanup(curl);
  43. return res;
  44. }
  45. // http POST
  46. CURLcode curl_post_req(const string &url, const string &postParams, string &response)
  47. {
  48. // init curl
  49. CURL *curl = curl_easy_init();
  50. // res code
  51. CURLcode res;
  52. if (curl)
  53. {
  54. // set params
  55. curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
  56. curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
  57. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params
  58. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
  59. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
  60. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  61. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  62. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
  63. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
  64. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  65. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  66. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  67. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  68. // start req
  69. res = curl_easy_perform(curl);
  70. }
  71. // release curl
  72. curl_easy_cleanup(curl);
  73. return res;
  74. }
  75. int main()
  76. {
  77. // global init
  78. curl_global_init(CURL_GLOBAL_ALL);
  79. // test get requery
  80. string getUrlStr = "http://cn.bing.com/images/trending?form=Z9LH";
  81. string getResponseStr;
  82. auto res = curl_get_req(getUrlStr, getResponseStr);
  83. if (res != CURLE_OK)
  84. cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl;
  85. else
  86. cout << getResponseStr << endl;
  87. // test post requery
  88. string postUrlStr = "https://www.baidu.com/s";
  89. string postParams = "f=8&rsv_bp=1&rsv_idx=1&word=picture&tn=98633779_hao_pg";
  90. string postResponseStr;
  91. auto res = curl_post_req(postUrlStr, postParams, postResponseStr);
  92. if (res != CURLE_OK)
  93. cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl;
  94. else
  95. cout << postResponseStr << endl;
  96. // global release
  97. curl_global_cleanup();
  98. system("pause");
  99. return 0;
  100. }
    • get和post可以用于请求html信息,也可以请求xml和json等串
    • 可以添加自定义的header 域和cookies
    • 这是libcurl的简单接口,基本等同于阻塞试请求,libcurl有高阶的异步并发接口,运用更复杂

http://blog.csdn.net/u012234115/article/details/71371962

C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)的更多相关文章

  1. (转) c/c++调用libcurl库发送http请求的两种基本用法

    libcurl主要提供了两种发送http请求的方式,分别是Easy interface方式和multi interface方式,前者是采用阻塞的方式发送单条数据,后者采用组合的方式可以一次性发送多条数 ...

  2. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  3. 【python接口自动化】- 使用requests库发送http请求

    前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...

  4. 『居善地』接口测试 — 4、Requests库发送GET请求

    目录 1.使用Requests库发送带参数的GET请求 2.查看GET请求的内容 3.带请求头.参数的Get请求 Requests库GET请求是使用HTTP协议中的GET请求方式对目标网站发起请求. ...

  5. 『居善地』接口测试 — 5、使用Requests库发送POST请求

    目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...

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

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

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

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

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

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

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

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

随机推荐

  1. Django项目开发实例之我的博客

    1.开发环境 2.创建virtualenv 3.安装相关包 Django Pillow 4.创建项目 添加应用: 5.设置静态文件和模板 6.运行测试 参考(http://www.cnblogs.co ...

  2. .NET-架构优化实战-梳理篇

    原文:.NET-架构优化实战-梳理篇 前言 程序员输出是他敲写的代码,那么输入就是他思考好的设计.因此不做设计是不存在,设计只分优秀的设计和糟糕的设计.为了避免过度设计浪费成本,需要针对现有业务与问题 ...

  3. source insight 添加自定义macro

    打开C:\Documents and Settings\xxxx\My Documents\Source Insight\Projects\Base文件夹下的em文件,可以看到都是由macro定义的一 ...

  4. C++网络编程方面的开源项目

    Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力. ...

  5. ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案

    原文:ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大 ...

  6. App各种Icon及Launch image的尺寸和用途

    App各种Icon及Launch image的尺寸和用途 IOS7,8 Asset iPhone 6 Plus (@3x) iPhone 6 and iPhone 5 (@2x) iPhone 4s ...

  7. Java冒泡排序与直接选择排序代码随笔

    冒泡排序:延申的有很多种,有的是先确定最大值放到后面,有的是先确定最小值放到前边,还有就是反过来,先确定最小值的位置,但是本质都是:不断两两比较,交换位置...第一趟确定一个最大(最小)值放到前边(后 ...

  8. Linux下新手怎样将VIM配置成C++编程环境(能够STL自己主动补全)

    ~ 弄拉老半天,最终弄的几乎相同啦,果然程序猿还是须要有点折腾精神啊. 首先你要安装vim,命令:sudo apt-get install vim vim它仅仅是一个编辑器,它不是IDE(比方code ...

  9. 常规容器下SpringBootServletInitializer如何实现web.xml作用解析

    在之前的<使用jsp作为视图模板&常规部署>章节有过一个实践,需要启动类继承自SpringBootServletInitializer方可正常部署至常规tomcat下,其主要能够起 ...

  10. Linux核心设计依据(六)该块I/O一层

    块设备是能随机存取装置固定大小的数据表设备.如硬盘:字符设备(如串口和键盘)它是按照字符流进入有序进行.不同之处在于是否足够的随机存取数据--这时候,你可以随心所欲地从一个位置跳到访问设备和位置.复杂 ...