vs2015下配置使用libcurl

布满荆棘的人生 2021-11-02 05:32 498阅读 0赞

首先通过nuget获取openssl和curl,不用加任何其它的东西,包括属性设置

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

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,498人围观)

还没有评论,来说两句吧...

相关阅读

    相关 VS2015的OpenCV配置

    OpenCV是开源计算机视觉库,它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法。opencv 拥有包括 300 多个 C 函数的跨平台