curl静态编译成供vs2015使用的静态库

1 下载curl源码

在GitHub 上可以下载curl源码,curl下载地址
这里我们下载最新版的curl-7.65.3

2 编译成静态库

以管理员身份运行vs2015 x86本机工具命令提示符
在这里插入图片描述
将curl压缩包解压,在vs2015命令提示符中进入解压后的winbuild目录
在这里插入图片描述
然后输入编译命令:

nmake /f Makefile.vc mode=static VC=14 RTLIBCFG=static ENABLE_IDN=no

注意命令中中一定ENABLE_IDN这个Windows的idn设为否。
编译完成后会在builds目录下生成3个文件夹,最上面的文件夹就是生成的curl静态库。

在这里插入图片描述
接下来我们就可以新建一个vs2015控制台程序,在程序里调用curl静态库了。
注意在调用静态库的过程中需要如下配置
C+±预处理中添加:
CURL_STATICLIB
链接器-输入中添加:
crypt32.lib
ws2_32.lib
wldap32.lib
winmm.lib
libcurl_a.lib
C++代码生成选择MT模式。
以下为测试代码:

//设定预处理器
#define CURL_STATICLIB
#include<iostream>
#include<string>
#include"curl\curl.h"

using namespace std;

/*
curl静态库的调用示例
*/

//链接器输入设置lib文件
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "libcurl_a.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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章