zoukankan      html  css  js  c++  java
  • 在Windows上使用libcurl发起HTTP请求

    curl下载地址https://curl.haxx.se/download.html

    当前最新版本为7.61.0

    将下载的curl-7.61.0.zip解压,得到curl-7.61.0

    在目录curl-7.61.0winbuild中打开cmd

    找到vcvars32.bat的位置(它在VS的安装目录中),在刚刚打开的cmd中运行此bat(目的是设置临时的环境变量,使当前的cmd可以运行nmake)

    在cmd中运行nmake /f Makefile.vc mode=dll VC=<VC版本>,例如nmake /f Makefile.vc mode=dll VC=14

    构建结束后,curl-7.61.0uildslibcurl-vc15-x86-release-dll-ipv6-sspi-winssl中的liblibcurl.libbinlibcurl.dllincludecurl*.h便是我们所需要的库和头文件

    GET示例

    #include <stdio.h>
    #include <curl/curl.h>
    #pragma comment(lib, "libcurl.lib")
    size_t writeFunction(void *data, size_t sizeOfEachObject, size_t numberOfObjects);
    int main()
    {
        curl_global_init(CURL_GLOBAL_ALL);
        CURL *curl = curl_easy_init();
        if (curl)
        {
            const char *url = "https://api.eyekey.com/face/Check/checking?app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=http%3A%2F%2Fpicview01.baomihua.com%2Fphotos%2F20120713%2Fm_14_634778197959062500_40614445.jpg";
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
            CURLcode curlCode = curl_easy_perform(curl);
            if (CURLE_OK != curlCode)
                fprintf(stderr, "curl_easy_perform() failed: %s
    ", curl_easy_strerror(curlCode));
            curl_easy_cleanup(curl);
        }
        else
            printf("Something went wrong
    ");
        curl_global_cleanup();
        system("pause");
        return 0;
    }
    size_t writeFunction(void *data, size_t sizeOfEachObject, size_t numberOfObjects)
    {
        char *dataPtr = (char *)data;
        dataPtr[numberOfObjects] = '';
        printf("%s", dataPtr);
        return sizeOfEachObject * numberOfObjects;
    }

    POST示例

    #include <stdio.h>
    #include <curl/curl.h>
    #pragma comment(lib, "libcurl.lib")
    int main()
    {
        curl_global_init(CURL_GLOBAL_ALL);
        CURL *curl = curl_easy_init();
        if (curl)
        {
            const char *url = "https://api.eyekey.com/face/Check/checking";
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=18bb0cc2dd1373f0f53f68999c342cc6/caef76094b36acafe725024570d98d1000e99c46.jpg");
            CURLcode curlCode = curl_easy_perform(curl);
            if (CURLE_OK != curlCode)
                fprintf(stderr, "curl_easy_perform() failed: %s
    ", curl_easy_strerror(curlCode));
            curl_easy_cleanup(curl);
        }
        curl_global_cleanup();
        system("pause");
        return 0;
    }

    参考链接

    Example libcurl GET request

    libcurl example - http-post.c

  • 相关阅读:
    Unity 摄像机Clear Flags和Culling Mask属性用途详解
    Unity 坐标系
    Unity 模型导入导出
    Unity 序列化
    正确理解静态Static关键字
    Unity 中的协同程序
    Asp.Net中调用存储过程并返回输出参数
    php学习知识点
    Jauery 中Ajax的几种异步请求
    2014年12月21号面试
  • 原文地址:https://www.cnblogs.com/buyishi/p/9362032.html
Copyright © 2011-2022 走看看