下载libCURL源码
使用VS2015命令行编译
编译32位静态库
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使用
// 导入头文件#include <WinSock2.h>#include <Ws2tcpip.h>#include <windows.h>#include <Wincrypt.h>// 链接WIN32的静态库#pragma comment(lib, "Ws2_32.lib")#pragma comment(lib, "winmm.lib")#pragma comment(lib, "wldap32.lib")#pragma comment(lib, "Advapi32.lib")// 用于使用静态库#define BUILDING_LIBCURL// 用于自动查找到CURL目录下的lib文件#define LIBPATH(p,f) p##f// 用于链接不同版本的静态库#ifdef QT_NO_DEBUG#pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl.lib"))#else#pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl_debug.lib"))#endif// 包含CURL 头文件#include "curl/curl.h"
一个帮助类
helper.h
#ifndef HUGOPEIXOTO_NET_REQUESTS_H_#define HUGOPEIXOTO_NET_REQUESTS_H_// 导入头文件#include <WinSock2.h>#include <Ws2tcpip.h>#include <windows.h>#include <Wincrypt.h>#include <map>#include <string>// 链接WIN32的静态库#pragma comment(lib, "Ws2_32.lib")#pragma comment(lib, "winmm.lib")#pragma comment(lib, "wldap32.lib")#pragma comment(lib, "Advapi32.lib")// 用于使用静态库#define BUILDING_LIBCURL// 用于自动查找到CURL目录下的lib文件#define LIBPATH(p,f) p##f// 用于链接不同版本的静态库#ifdef QT_NO_DEBUG#pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl.lib"))#else#pragma comment(lib, LIBPATH(__FILE__, ".dir/../libcurl_debug.lib"))#endif// 包含CURL 头文件#include "curl/curl.h"namespace requests {typedef std::multimap<std::string, std::string> Headers;struct Response {uint32_t status_code;Headers headers;std::string body;Response();};struct Request {std::string method;std::string url;std::string proxyURL;Headers headers;std::string body;};Response get(std::string url, const std::string proxyURL="", Headers = {});Response post(std::string url, const std::string &body, const std::string proxyURL="", Headers = {});Response put(std::string url, const std::string &body, const std::string proxyURL="", Headers = {});Response call(const Request &req);}#endif
helper.cpp
#include "requests.h"static int body_writer(char *data, size_t size, size_t nmemb,requests::Response *response) {response->body.append(data, size * nmemb);return size * nmemb;}static int header_writer(char *data, size_t size, size_t nmemb,requests::Response *response) {std::string header(data, size*nmemb);auto idx = header.find(':');if (idx != std::string::npos && idx > 0 && idx + 4 < header.size()) {response->headers.insert(std::make_pair(header.substr(0, idx),header.substr(idx+2, header.size()-idx-4)));}return size * nmemb;}requests::Response::Response() { status_code = 0; }requests::Response requests::get(std::string url, const std::string proxyURL, Headers headers) {Request req;req.method = "GET";req.url = url;req.headers = headers;req.proxyURL = proxyURL;return call(req);}requests::Response requests::post(std::string url, const std::string &body,const std::string proxyURL, Headers headers) {Request req;req.method = "POST";req.url = url;req.headers = headers;req.body = body;req.proxyURL = proxyURL;return call(req);}requests::Response requests::put(std::string url, const std::string &body,const std::string proxyURL, Headers headers) {Request req;req.method = "PUT";req.url = url;req.headers = headers;req.body = body;req.proxyURL = proxyURL;return call(req);}requests::Response requests::call(const Request &request) {requests::Response response;CURL *curl = curl_easy_init();struct curl_slist *headers = nullptr;for (auto header : request.headers) {std::string header_line = header.first + ": " + header.second;headers = curl_slist_append(headers, header_line.c_str());}curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.method.c_str());curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str());curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);// curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);// curl_easy_setopt(curl, CURLOPT_ENCODING, "identity");curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);curl_easy_setopt(curl, CURLOPT_HEADER, 0);if(request.proxyURL.length() > 0){curl_easy_setopt(curl, CURLOPT_PROXY, request.proxyURL.c_str());}curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_writer);curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &response);curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_writer);curl_easy_perform(curl);curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response.status_code);curl_slist_free_all(headers);curl_easy_cleanup(curl);return response;}
