C + + uses the Libcurl library to send HTTP requests (get and post can be used to request HTML information or to request XML and JSON strings)

Source: Internet
Author: User
Tags http post

C + + to implement an HTTP network connection, the need for third-party libraries, libcurl use is still very convenient

Environment: Win32 + VS2015

If you want to use it under Linux, it's basically the same

1, download compile Libcurl

Download the curl source, find the VS project, follow the x86 x64 and compile the static lib with the corresponding debug and release

2, building the project

1) Curl header file and Lib Copy to project directory

2) Configure the Lib directory in the Include and additional library directories Libcurl in the additional include directory Libcurl

3) Add pre-compiled macros Use_openssl and Curl_staticlib

4) Add as dependent libraries

Crypt32.lib
Ws2_32.lib
Wldap32.lib
Libcurl.lib

Note the version corresponds

3, code example

[CPP]View PlainCopyprint?
  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 and post can be used to request HTML information, or you can request XML and JSON strings
      • Custom header fields and cookies can be added
      • This is the simple interface of Libcurl, basically the same as the blocking test request, Libcurl has the higher order asynchronous concurrency interface, the use more complex

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

C + + uses the Libcurl library to send HTTP requests (get and post can be used to request HTML information or to request XML and JSON strings)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.