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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章