在GitHub上我们可以下载curl的源码,源码可以编译成供我们特定编译工具使用的动态库,例如编译成vs2011、vs2013、vs2015以及vs2017。下面将以编译成vs2015进行讲解,编译成其他版本的和这个过程差不多。
在GitHub 上可以下载curl源码,网页地址
我们在这个网页上下载最新版本,curl-7.64.1.zip
由于需要使用vs2015中vc环境下的nmake工具,因此需要将nmake添加到环境变量path目录下。
我的电脑vs2015是安装在D盘下,首先找到nmake所在目录
我的电脑nmake所在目录为D:\software\vs2015\VC\bin
将这个目录添加到环境变量的Path下
将电脑重启,使环境变量生效。
以管理员身份运行VS2015 x64 本机工具命令提示符
将curl-7.64.1.zip进行解压,在VS2015 x64 本机工具命令提示符中进入到curl解压目录下的winbuild目录下,也就是这个目录
h:
cd H:\curl-7.64.1
cd winbuild
nmake /f Makefile.vc mode=dll VC=14 MACHINE=x64 DEBUG=no
等待编译完成。
编译完成后会在builds目录下生成动态库
进入builds目录会发现目录下有3个文件夹,生成的动态库在最上面那个文件夹里。
在这个目录下bin文件夹为dll动态库,include为头文件,lib为lib文件。
下面链接为自己编译的curl动态库
下载地址:
https://download.csdn.net/download/qq_37781464/11168416
只需要以管理员权限运行vs2015 x86本机工具命令提示符,使用编译命令:
nmake /f Makefile.vc mode=dll VC=14 MACHINE=x86 DEBUG=no
最后会在builds目录下生成32位的动态库
测试源码如下:
#include
#include
#include"curl\curl.h"
using namespace std;
/*
curl动态库的调用示例
*/
//链接器输入设置lib文件
#pragma comment(lib, "libcurl.lib")
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream);
CURLcode curl_get_req(const std::string &url, std::string &response);
CURLcode curl_post_req(const string &url, const string &postParams, string &response);
int main()
{
curl_global_init(CURL_GLOBAL_ALL);
//post请求和get请求应该分开测
//get请求测试
/*
string getUrlStr = "http://www.baidu.com";
string getResponseStr;
auto res = curl_get_req(getUrlStr, getResponseStr);
if (res != CURLE_OK)
cout << "get error" << endl;
else
cout << getResponseStr << endl;
*/
//post请求测试
string url_post0 = "https://fpdk.shaanxi.chinatax.gov.cn/NSbsqWW/login.do";
string postparams = "type=CLIENT-HELLO&clientHello=1111&ymbb=3.1.01";
string resPost0;
auto res3 = curl_post_req(url_post0, postparams, resPost0);
cout << res3 << endl;
if (res3 != CURLE_OK)
cout << "post error" << endl;
else
{
cout << resPost0 << endl;
}
curl_global_cleanup();
getchar();
return 0;
}
// reply of the requery
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
{
//在注释的里面可以打印请求流,cookie的信息
//cout << "----->reply" << endl;
string *str = (string*)stream;
//cout << *str << endl;
(*str).append((char*)ptr, size*nmemb);
return size * nmemb;
}
// http GET
CURLcode curl_get_req(const std::string &url, std::string &response)
{
// init curl
CURL *curl = curl_easy_init();
// res code
CURLcode res;
if (curl)
{
//设置curl的请求头
struct curl_slist* header_list = NULL;
header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
// set params
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
//设置请求头
curl_easy_setopt(curl, CURLOPT_HEADER, header_list);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
// start req
res = curl_easy_perform(curl);
}
// release curl
curl_easy_cleanup(curl);
return res;
}
// http POST
CURLcode curl_post_req(const string &url, const string &postParams, string &response)
{
// init curl
CURL *curl = curl_easy_init();
// res code
CURLcode res;
if (curl)
{
// set params
//设置curl的请求头
struct curl_slist* header_list = NULL;
header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_HEADER, header_list);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 4);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 4);
// start req
res = curl_easy_perform(curl);
}
// release curl
curl_easy_cleanup(curl);
return res;
}
自己制作的动态库及测试程序资源下载地址:libcurl动态库及测试程序.rar