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

原创
小哥 2年前 (2023-05-24) 阅读数 37 #大杂烩

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 

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) + ")

" + 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;
}
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门