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

2023-11-10

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,代码示例

[cpp]  view plain  copy
 
 print?


  1. #include <iostream>  
  2. #include <string>  
  3. #include “curl/curl.h”  
  4. using namespace std;  
  5.   
  6. #pragma comment(lib, “ws2_32.lib”)  
  7. #pragma comment(lib, “wldap32.lib”)  
  8. #pragma comment(lib, “libcurl.lib”)  
  9.   
  10. // reply of the requery  
  11. size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)  
  12. {  
  13.     cout << “—–>reply” << endl;  
  14.     string str = (string)stream;  
  15.     cout << *str << endl;  
  16.     (str).append((char)ptr, size*nmemb);  
  17.     return size * nmemb;  
  18. }  
  19.   
  20. // http GET  
  21. CURLcode curl_get_req(const std::string &url, std::string &response)  
  22. {  
  23.     // init curl  
  24.     CURL *curl = curl_easy_init();  
  25.     // res code  
  26.     CURLcode res;  
  27.     if (curl)  
  28.     {  
  29.         // set params  
  30.         curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url  
  31.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https  
  32.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false  
  33.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  34.         curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  35.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);  
  36.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);  
  37.         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  38.         curl_easy_setopt(curl, CURLOPT_HEADER, 1);  
  39.         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time  
  40.         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  41.         // start req  
  42.         res = curl_easy_perform(curl);  
  43.     }  
  44.     // release curl  
  45.     curl_easy_cleanup(curl);  
  46.     return res;  
  47. }  
  48.   
  49. // http POST  
  50. CURLcode curl_post_req(const string &url, const string &postParams, string &response)  
  51. {  
  52.     // init curl  
  53.     CURL *curl = curl_easy_init();  
  54.     // res code  
  55.     CURLcode res;  
  56.     if (curl)  
  57.     {  
  58.         // set params  
  59.         curl_easy_setopt(curl, CURLOPT_POST, 1); // post req  
  60.         curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url  
  61.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params  
  62.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https  
  63.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false  
  64.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  65.         curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  66.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);  
  67.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);  
  68.         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  69.         curl_easy_setopt(curl, CURLOPT_HEADER, 1);  
  70.         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  71.         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  72.         // start req  
  73.         res = curl_easy_perform(curl);  
  74.     }  
  75.     // release curl  
  76.     curl_easy_cleanup(curl);  
  77.     return res;  
  78. }  
  79.   
  80. int main()  
  81. {  
  82.     // global init  
  83.     curl_global_init(CURL_GLOBAL_ALL);  
  84.   
  85.     // test get requery  
  86.     string getUrlStr = http://cn.bing.com/images/trending?form=Z9LH“;  
  87.     string getResponseStr;  
  88.     auto res = curl_get_req(getUrlStr, getResponseStr);  
  89.     if (res != CURLE_OK)  
  90.         cerr << “curl_easy_perform() failed: ” + string(curl_easy_strerror(res)) << endl;  
  91.     else  
  92.         cout << getResponseStr << endl;  
  93.   
  94.     // test post requery  
  95.     string postUrlStr = https://www.baidu.com/s“;  
  96.     string postParams = “f=8&rsv_bp=1&rsv_idx=1&word=picture&tn=98633779_hao_pg”;  
  97.     string postResponseStr;  
  98.     auto res = curl_post_req(postUrlStr, postParams, postResponseStr);  
  99.     if (res != CURLE_OK)  
  100.         cerr << “curl_easy_perform() failed: ” + string(curl_easy_strerror(res)) << endl;  
  101.     else  
  102.         cout << postResponseStr << endl;  
  103.   
  104.     // global release  
  105.     curl_global_cleanup();  
  106.     system(“pause”);  
  107.     return 0;  
  108. }  



 

    • get和post可以用于请求html信息,也可以请求xml和json等串
    • 可以添加自定义的header 域和cookies
    • 这是libcurl的简单接口,基本等同于阻塞试请求,libcurl有高阶的异步并发接口,运用更复杂

 

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 同源策略与跨域

    前言 最近业务上前端同学多次联系说访问腾讯云cos资源的时候因为跨域的问题访问不到 大致看了下腾讯云关于设置跨域访问的教程 按照前端同学给的域名等选项就给配了 而且测试下来也是好的 但是呢一直不知道什么是跨域这里就做一个简单的学习记录 一
  • 在 Mac 上安装 pycurl

    我对 python 非常陌生 需要帮助在我的机器上安装 pycurl 库 我现在正在运行 python 2 7 一个简短的教程将不胜感激 使用两种方法之一 方法一 须藤easy install pycurl 方法二 pip 安装 pycur
  • 如何构建自定义 libcurl 以仅支持 HTTP / HTTPS 协议

    我仅使用 libcurl 来发出 HTTP HTTPS 请求 我已经下载了源代码并构建为静态库 我的最终可执行文件的大小有所增加 我正在尝试减少我的可执行文件 因为我有其他限制 在分析过程中 我观察到 libcurl 是支持许多协议 包括
  • 在curl_easy_perform之后接收数据

    我有以下问题 如何编写通过 http response 返回的数据char 缓冲 我发现了几种方法 use CURLOPT WRITEDATA or CURLOPT WRITEFUNCTION but CURLOPT WRITEDATA需要
  • C++:Libcurlcurl_easy_init() 给出访问冲突错误并使程序崩溃

    我正在尝试将 libcurl 与我正在编写的程序一起使用 但我遇到了一些问题 到目前为止 我只尝试了 libcurl 网站上的示例 但是一旦程序进入curl 初始化 它们就会崩溃 我当前的代码 include
  • Windows 上的卷曲

    我已关注此链接上的所有内容 如何在 Windows 上安装 设置和使用 cURL 但我无法安装最新的curl 7 50 3https curl haxx se download html 在 Windows 7 2008 Server R2
  • 从 gchar 缓冲区发送图像到 ftp 服务器 (libcurl)

    我正在开发一个用 C 编程的 Linux 应用程序 它处理 gdk pixbuf 图像 然后通过 ftp libcurl 将它们发送到远程服务器 图像保存到 gchar 缓冲区中gdk pixbuf save to buffer 问题是我不
  • 为什么 libcurl 不为 IP 设置 SNI?

    我刚刚注意到当我使用 IP 进行 HTTPS 调用时 libcurl 没有设置 SNI 字段 我找到了这个 https github com curl curl blame master lib vtls openssl c ifdef S
  • 如何在MacOS10.12下安装libcurl并用于Xcode?

    为 iOS 4 2 构建 libCURL http www creativealgorithms com blog content building libcurl ios 42 大家好 这是我的第一个堆栈溢出问题 我已经阅读了之前发布的博
  • cURL 在链中使用多个代理

    是否可以使用 cURL 在单个请求中链接多个代理 例如 启动 cURL gt proxy1 gt proxy2 gt 目标地址 使用 cURL 可以实现这一点吗 根据定义 代理是中间人 在客户端和服务器之间运行和工作的软件 客户端询问代理
  • 在 Mac 上使用 C 语言时的 LibCURL

    基本上 我尝试简单地使用 libCURL 来下载网站 并且我一直在使用以下代码 include
  • 在 Linux 中将 RCurl 安装为 R 开发工具的一部分时出错

    首先 我对 Linux 还很陌生 我在 Linux 服务器 Ubuntu 12 10 Quantal Quetzal 上安装了 R 3 1 1 最新版本 并尝试在 R 中安装 devtools 包 当我运行install packages
  • 使用 C++ 的简单 HTTP 请求

    在您将此标记为冗余之前 请注意我已经尝试了网上发布的许多方法 包括堆栈溢出 但它们都无法满足我的需求 另请注意 我对编程世界还很陌生 所以请原谅我滥用技术术语 现在我正在编写一个 C 程序 它计算来自用户计算机的一些数据 例如 IP 地址
  • PHP cURL、POST JSON

    我需要发布以下 JSON 代码 但由于某种原因它不起作用 下面是我的代码 fieldString 395609399 the curl request processor function processCurlJsonrequest UR
  • curl - 如何设置 DNS 缓存的 TTL 以及如何清除curl 缓存

    在这个链接的最后它提到 http comments gmane org gmane comp web curl library 40895 只要您重新使用该句柄 libcurl 默认就会使用其 DNS 缓存 你 可以改变它在缓存中保存条目的
  • libCurl 和 OpenSSL 的奇怪之处

    又是我 我就是不明白 为什么会这样 我下载了 OpenSSL 的编译静态库 从这里 http www shininglightpro com download Win32OpenSSL 1 0 0d exe 此链接位于 cUrl 官方网站的
  • 使用curl和php自动填写表单

    我正在尝试编写一个自动填写表单然后自动按下提交按钮的脚本 我读过您可以使用curl来发布HTTP请求 但是当表单使用JavaScript处理post请求时 您会做什么 就像下面的代码一样
  • libcurl 回调 w/c++ 类成员

    取自libcurl 编程教程 http curl haxx se libcurl c libcurl tutorial html在 libcurl 网站上 libcurl 与 C 使用 C 时基本上只需要记住一件事 在连接 libcurl
  • 从标准 cookie 格式转换为 LibCurl cookie jar 格式

    有没有方便的工具 代码可以转换标准 cookie 字符串格式 e g NAME1 VALUE1 NAME2 VALUE2 to the libCURL cookie jar 格式 e g netscape com TRUE FALSE 94
  • 为 libcurl 添加自签名 SSL 证书

    我在我的 C 应用程序中使用 libcurl 与我设置的 HTTPS 服务器进行通信 我在该服务器上生成了一个自签名证书 我希望将其与curl 一起使用 我知道将 CURLOPT SSL VERIFYPEER 设置为 0 可以绕过 SSL

随机推荐