Libcurl在Windows平台仅用Http模块的静态库编译和使用

博客介绍了在C++中使用libcurl库的具体步骤。首先下载当前版本7.84的源码并解压,在projects\\Windows\\目录选择对应vs版本打开libcurl项目,以lib debug编译配置生成libcurld.lib。接着在使用库的项目中包含头文件,进行链接配置,同时添加ws2_32.lib、wldap32.lib,最后定义宏#define CURL_STATICLIB。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 下载源码 curl - Download,当前版本7.84

2 解压仅projects\Windows\目录,选择对应的vs版本,打开

3 libcurl项目,选择lib debug 编译配置,生成,得到libcurld.lib

4 使用库的项目中包含头文件 在curl源码目录下include\curl

5 链接配置libcurld.lib目录及引入文件,这里还需要同时添加ws2_32.lib、wldap32.lib

6 引入头文件的地方定义宏 #define CURL_STATICLIB

示例:

#include "stdafx.h"
#define CURL_STATICLIB
#include "curl.h"
#include <string>

using namespace std;

struct stData
{
	char* pBuf;
	size_t nLen;		
	size_t nDataLen;	
	stData()
	{
		pBuf = NULL;
		nLen = 0;
		nDataLen = 0;
	}
	~stData()
	{
		if (pBuf != NULL)
		{
			delete pBuf;
			pBuf = NULL;
		}
	}
};

size_t on_data(void* buffer, size_t sz, size_t szn, void* stream)
{
	size_t nDataSize = sz * szn;

	stData* pData = (stData*)stream;
	size_t nFreeSize = pData->nLen - pData->nDataLen;

	if (nFreeSize <= nDataSize)
	{
		while (pData->nLen <= pData->nDataLen + nDataSize)
		{
			if (pData->nLen == 0)
			{
				pData->nLen = 1024 * 8;
			}
			else if (pData->nLen == 1024 * 8)//8k
			{
				pData->nLen = 1024 * 64;
			}
			else if (pData->nLen == 1024 * 64)//64k
			{
				pData->nLen = 1024 * 512;
			}
			else if (pData->nLen == 1024 * 512)//512k
			{
				pData->nLen = 1024 * 1024 * 2;
			}
			else if (pData->nLen == 1024 * 1024 * 2)//2M
			{
				pData->nLen = 1024 * 1024 * 4;
			}
			else if (pData->nLen >= 1024 * 1024 * 4)//4M
			{
				pData->nLen += 1024 * 16;	
			}
		}

		char* pBuf = new char[pData->nLen];
		memset(pBuf, 0, pData->nLen);
		if (pData->nDataLen > 0)
		{
			memcpy_s(pBuf, pData->nLen, pData->pBuf, pData->nDataLen);
		}
		if (pData->pBuf != NULL)
		{
			delete pData->pBuf;
		}
		pData->pBuf = pBuf;
	}

	memcpy_s(pData->pBuf + pData->nDataLen, (pData->nLen - pData->nDataLen), (char*)buffer, nDataSize);
	pData->nDataLen += nDataSize;
	return sz * szn;
}

bool DownloadData(const string& strURL, string& strMsg,struct stData  &theData)
{
	if (strURL == "")
	{
		strMsg = "elBlackList.DownloadData:URL empty";
		return false;
	}

	CURL* curl = curl_easy_init();
	if (!curl)
	{
		strMsg = "elBlackList.DownloadData:curl_easy_init error";
		return false;
	}

	string sURL = strURL;
	CURLcode res1=curl_easy_setopt(curl, CURLOPT_URL, sURL.c_str());//设置下载地址
	curl_easy_setopt(curl, CURLOPT_POST, 0); //Get 请求
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);//设置超时时间
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_data);//设置写数据的函数
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &theData);//设置写数据的变量

	CURLcode res = curl_easy_perform(curl);//执行下载
	curl_easy_cleanup(curl);//释放curl资源
	curl = NULL;

	if (res != CURLE_OK)
	{
		strMsg = "elBlackList.DownloadData:curl_easy_perform error(" + to_string(res) + ")\n" + sURL;
		return false;
	}
	return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
	string strMsg = "";
	struct stData theData;
	string BlackListUrl = "http://192.168.1.123/getdata.ashx";
	bool res = DownloadData(BlackListUrl, strMsg,theData);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值