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

原文链接: https://www.cnblogs.com/findumars/p/7252843.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/257541

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午10:59
下一篇 2023年2月14日 上午11:00

相关推荐