下载libCURL源码

使用VS2015命令行编译

编译32位静态库

  1. nmake /f Makefile.vc mode=static ENABLE_IPV6=no DEBUG=yes MACHINE=x86 VC=14 ENABLE_SSPI=no ENABLE_IDN=no ENABLE_WINSSL=no

导入QT使用

  1. // 导入头文件
  2. #include <WinSock2.h>
  3. #include <Ws2tcpip.h>
  4. #include <windows.h>
  5. #include <Wincrypt.h>
  6. // 链接WIN32的静态库
  7. #pragma comment(lib, "Ws2_32.lib")
  8. #pragma comment(lib, "winmm.lib")
  9. #pragma comment(lib, "wldap32.lib")
  10. #pragma comment(lib, "Advapi32.lib")
  11. // 用于使用静态库
  12. #define BUILDING_LIBCURL
  13. // 用于自动查找到CURL目录下的lib文件
  14. #define LIBPATH(p,f) p##f
  15. // 用于链接不同版本的静态库
  16. #ifdef QT_NO_DEBUG
  17. #pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl.lib"))
  18. #else
  19. #pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl_debug.lib"))
  20. #endif
  21. // 包含CURL 头文件
  22. #include "curl/curl.h"

一个帮助类

helper.h

  1. #ifndef HUGOPEIXOTO_NET_REQUESTS_H_
  2. #define HUGOPEIXOTO_NET_REQUESTS_H_
  3. // 导入头文件
  4. #include <WinSock2.h>
  5. #include <Ws2tcpip.h>
  6. #include <windows.h>
  7. #include <Wincrypt.h>
  8. #include <map>
  9. #include <string>
  10. // 链接WIN32的静态库
  11. #pragma comment(lib, "Ws2_32.lib")
  12. #pragma comment(lib, "winmm.lib")
  13. #pragma comment(lib, "wldap32.lib")
  14. #pragma comment(lib, "Advapi32.lib")
  15. // 用于使用静态库
  16. #define BUILDING_LIBCURL
  17. // 用于自动查找到CURL目录下的lib文件
  18. #define LIBPATH(p,f) p##f
  19. // 用于链接不同版本的静态库
  20. #ifdef QT_NO_DEBUG
  21. #pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl.lib"))
  22. #else
  23. #pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl_debug.lib"))
  24. #endif
  25. // 包含CURL 头文件
  26. #include "curl/curl.h"
  27. namespace requests {
  28. typedef std::multimap<std::string, std::string> Headers;
  29. struct Response {
  30. uint32_t status_code;
  31. Headers headers;
  32. std::string body;
  33. Response();
  34. };
  35. struct Request {
  36. std::string method;
  37. std::string url;
  38. std::string proxyURL;
  39. Headers headers;
  40. std::string body;
  41. };
  42. Response get(std::string url, const std::string proxyURL="", Headers = {});
  43. Response post(std::string url, const std::string &body, const std::string proxyURL="", Headers = {});
  44. Response put(std::string url, const std::string &body, const std::string proxyURL="", Headers = {});
  45. Response call(const Request &req);
  46. }
  47. #endif

helper.cpp

  1. #include "requests.h"
  2. static int body_writer(char *data, size_t size, size_t nmemb,
  3. requests::Response *response) {
  4. response->body.append(data, size * nmemb);
  5. return size * nmemb;
  6. }
  7. static int header_writer(char *data, size_t size, size_t nmemb,
  8. requests::Response *response) {
  9. std::string header(data, size*nmemb);
  10. auto idx = header.find(':');
  11. if (idx != std::string::npos && idx > 0 && idx + 4 < header.size()) {
  12. response->headers.insert(std::make_pair(
  13. header.substr(0, idx),
  14. header.substr(idx+2, header.size()-idx-4)));
  15. }
  16. return size * nmemb;
  17. }
  18. requests::Response::Response() { status_code = 0; }
  19. requests::Response requests::get(std::string url, const std::string proxyURL, Headers headers) {
  20. Request req;
  21. req.method = "GET";
  22. req.url = url;
  23. req.headers = headers;
  24. req.proxyURL = proxyURL;
  25. return call(req);
  26. }
  27. requests::Response requests::post(std::string url, const std::string &body,
  28. const std::string proxyURL, Headers headers) {
  29. Request req;
  30. req.method = "POST";
  31. req.url = url;
  32. req.headers = headers;
  33. req.body = body;
  34. req.proxyURL = proxyURL;
  35. return call(req);
  36. }
  37. requests::Response requests::put(std::string url, const std::string &body,
  38. const std::string proxyURL, Headers headers) {
  39. Request req;
  40. req.method = "PUT";
  41. req.url = url;
  42. req.headers = headers;
  43. req.body = body;
  44. req.proxyURL = proxyURL;
  45. return call(req);
  46. }
  47. requests::Response requests::call(const Request &request) {
  48. requests::Response response;
  49. CURL *curl = curl_easy_init();
  50. struct curl_slist *headers = nullptr;
  51. for (auto header : request.headers) {
  52. std::string header_line = header.first + ": " + header.second;
  53. headers = curl_slist_append(headers, header_line.c_str());
  54. }
  55. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.method.c_str());
  56. curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str());
  57. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  58. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
  59. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  60. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
  61. // curl_easy_setopt(curl, CURLOPT_ENCODING, "identity");
  62. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
  63. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  64. curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  65. if(request.proxyURL.length() > 0)
  66. {
  67. curl_easy_setopt(curl, CURLOPT_PROXY, request.proxyURL.c_str());
  68. }
  69. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  70. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_writer);
  71. curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &response);
  72. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_writer);
  73. curl_easy_perform(curl);
  74. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response.status_code);
  75. curl_slist_free_all(headers);
  76. curl_easy_cleanup(curl);
  77. return response;
  78. }