C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
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,代码示例
- #include <iostream>
- #include <string>
- #include "curl/curl.h"
- using namespace std;
- #pragma comment(lib, "ws2_32.lib")
- #pragma comment(lib, "wldap32.lib")
- #pragma comment(lib, "libcurl.lib")
- // reply of the requery
- size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- cout << "----->reply" << endl;
- string *str = (string*)stream;
- cout << *str << endl;
- (*str).append((char*)ptr, size*nmemb);
- return size * nmemb;
- }
- // http GET
- CURLcode curl_get_req(const std::string &url, std::string &response)
- {
- // init curl
- CURL *curl = curl_easy_init();
- // res code
- CURLcode res;
- if (curl)
- {
- // set params
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- // start req
- res = curl_easy_perform(curl);
- }
- // release curl
- curl_easy_cleanup(curl);
- return res;
- }
- // http POST
- CURLcode curl_post_req(const string &url, const string &postParams, string &response)
- {
- // init curl
- CURL *curl = curl_easy_init();
- // res code
- CURLcode res;
- if (curl)
- {
- // set params
- curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- // start req
- res = curl_easy_perform(curl);
- }
- // release curl
- curl_easy_cleanup(curl);
- return res;
- }
- int main()
- {
- // global init
- curl_global_init(CURL_GLOBAL_ALL);
- // test get requery
- string getUrlStr = "http://cn.bing.com/images/trending?form=Z9LH";
- string getResponseStr;
- auto res = curl_get_req(getUrlStr, getResponseStr);
- if (res != CURLE_OK)
- cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl;
- else
- cout << getResponseStr << endl;
- // test post requery
- string postUrlStr = "https://www.baidu.com/s";
- string postParams = "f=8&rsv_bp=1&rsv_idx=1&word=picture&tn=98633779_hao_pg";
- string postResponseStr;
- auto res = curl_post_req(postUrlStr, postParams, postResponseStr);
- if (res != CURLE_OK)
- cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl;
- else
- cout << postResponseStr << endl;
- // global release
- curl_global_cleanup();
- system("pause");
- return 0;
- }
- 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等串)的更多相关文章
- (转) c/c++调用libcurl库发送http请求的两种基本用法
libcurl主要提供了两种发送http请求的方式,分别是Easy interface方式和multi interface方式,前者是采用阻塞的方式发送单条数据,后者采用组合的方式可以一次性发送多条数 ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- 【python接口自动化】- 使用requests库发送http请求
前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...
- 『居善地』接口测试 — 4、Requests库发送GET请求
目录 1.使用Requests库发送带参数的GET请求 2.查看GET请求的内容 3.带请求头.参数的Get请求 Requests库GET请求是使用HTTP协议中的GET请求方式对目标网站发起请求. ...
- 『居善地』接口测试 — 5、使用Requests库发送POST请求
目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...
- C++ 用libcurl库进行http通讯网络编程
使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...
- C++ 用libcurl库进行http通讯网络编程(转)
转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...
- C++ 用libcurl库进行http通讯网络编程[转]
http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...
- C/C++ 用libcurl库进行http通讯网络编程
C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...
随机推荐
- AE指定字段转成注记
转自原文 ae指定字段转成注记 ArcMap中有一个功能是Label Features,就是可以将图层内指定字段值显示以Label形式显示在主窗口上,在Label Features后,用右键点击图层, ...
- 微信测试号开发之九 微信网页授权:页面获取用户openid
原文链接:https://blog.csdn.net/qq_37936542/article/details/78981369 一:配置接口 注意:这里填写的是域名(是一个字符串),而不是URL,因此 ...
- [ES6] Use ES6 Proxies
A JavaScript Proxy allows you to intercept operations performed on objects, arrays, or functions lik ...
- php课程 4-16 数组自定义函数(php数组->桶)
php课程 4-16 数组自定义函数(php数组->桶) 一.总结 一句话总结:php的数组储存机制,和桶排序完美的结合.所以php的操作中多想多桶的操作. 二.数组自定义函数 1.相关知识 ...
- 如何安全退出多个Activity
我们在项目开发的时候可能会遇到安全退出应用的场景,如何能够安全退出多个Activity?网上有很多方法,如下: 1.抛异常退出 该方法通过抛异常,使程序Force Close.验证可以,但是,需要解决 ...
- [React Router v4] Use the React Router v4 Link Component for Navigation Between Routes
If you’ve created several Routes within your application, you will also want to be able to navigate ...
- PhpStorm常用快捷键(不多够用)
PhpStorm常用快捷键(不多够用) 一.总结 一句话总结: 1.前进 解答:Ctrl+shift+ z 2.跳转到指定行数 解答:Ctrl+G 3.块注释 解答:ctrl + shift + / ...
- .NET Core微服务之路:不断更新中的目录 (v0.43)
原文:.NET Core微服务之路:不断更新中的目录 (v0.43) 微服务架构,对于从事JAVA架构的童鞋来说,早已不是什么新鲜的事儿,他们有鼎鼎大名的Spring Cloud这样的全家桶框架支撑, ...
- 【t097】寄存器
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 蠕虫是一个古老的电脑游戏,它有许多版本.但所有版本都有一个共同的规则:操纵一 条蠕虫在屏幕上转圈,并试 ...
- Erlang OTP编程初体验——gen_server和行为模式
http://blog.sina.com.cn/s/blog_3fe961ae0101k4p6.html 行为模式其实非常类似于面向对象语言中的接口,至少笔者是这么理解的.OTP行为模式将一些反复出现 ...