如何在Native侧集成三方库Curl，并进行HTTP数据请求
===============================

可以将Curl移植到HarmonyOS，并在Native侧开发时直接使用Curl的C++库实现。具体的移植方法请参考相关链接。

具体使用步骤如下：

1.  将移植后的Curl的so库放入Native工程的entry/libs/arm64-v8a/目录，将包含头文件的include目录放入entry/src/main/cpp目录。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/58/v3/aqYrNOv0T3-RaRePtLrluw/zh-cn_image_0000002624475894.png?HW-CC-KV=V1&HW-CC-Date=20260709T133831Z&HW-CC-Expire=86400&HW-CC-Sign=81A0F01342403644034AA191E88F2208BD1B9457546D453E0C1DD34A8DAB94F1 "点击放大")
    
2.  在CMakeLists.txt文件中链接Curl对应的so库。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/8d/v3/HsmEJJVPQWmG1UNQRH7vcw/zh-cn_image_0000002654835203.png?HW-CC-KV=V1&HW-CC-Date=20260709T133831Z&HW-CC-Expire=86400&HW-CC-Sign=628377977D3C487371FD2789EAEF5F36CE891D056AC53B05EB8EEC5FE8D74B05 "点击放大")
    
3.  在Native侧的.cpp文件中引入头文件curl.h，使用Curl的相关能力。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/24/v3/btwzJ9QGSlG8v4EX397Mdw/zh-cn_image_0000002654795267.png?HW-CC-KV=V1&HW-CC-Date=20260709T133831Z&HW-CC-Expire=86400&HW-CC-Sign=B4C4E31E1EE33DABE23C0DEE08ED2EF95715B238A583E81E1D27982C0D1E1F6B "点击放大")
    
    具体可参考以下代码：
    
    #include "curl/curl.h"
    
    _// ..._
    
    _// Get request and post request data response functions_
    size\_t ReqReply(void \*ptr, size\_t size, size\_t nmemb, void \*userdata) {
        string \*str = reinterpret\_cast<string \*>(userdata);
        (\*str).append((char \*)ptr, size \* nmemb);
        return size \* nmemb;
    }
    
    _// http GET Request configuration_
    CURLcode CurlGetReq(const std::string &url, std::string &response) {
       _ // Curl initialization_
        CURL \*curl = curl\_easy\_init();
       _ // Curl return value_
        CURLcode res;
        if (curl) {
           _ // Set the request header for Curl_
            struct curl\_slist \*headers = NULL;
            headers = curl\_slist\_append(headers, "Content-Type:application/json");
            curl\_easy\_setopt(curl, CURLOPT\_HTTPHEADER, headers);
    
           _ // Set the URL address for the request_
            curl\_easy\_setopt(curl, CURLOPT\_URL, url.c\_str());
    
          _  // Receive response header data, 0 represents not receiving, 1 represents receiving_
            curl\_easy\_setopt(curl, CURLOPT\_HEADER, 1);
    
          _  // Set data receiving function_
            curl\_easy\_setopt(curl, CURLOPT\_WRITEFUNCTION, ReqReply);
            curl\_easy\_setopt(curl, CURLOPT\_WRITEDATA, (void \*)&response);
    
          _  // Set to not use any signal/alarm handlers_
            curl\_easy\_setopt(curl, CURLOPT\_NOSIGNAL, 1);
    
          _  // Set timeout period_
            curl\_easy\_setopt(curl, CURLOPT\_CONNECTTIMEOUT, 10);
            curl\_easy\_setopt(curl, CURLOPT\_TIMEOUT, 10);
    
           _ // Open request_
            res = curl\_easy\_perform(curl);
        }
       _ // Release curl_
        curl\_easy\_cleanup(curl);
        return res;
    }
    
    static napi\_value NatReq(napi\_env env, napi\_callback\_info info) {
        string getUrlStr = "http://app.huawei.com";
        string getResponseStr;
        auto res = CurlGetReq(getUrlStr, getResponseStr);
        if (res == CURLE\_OK) {
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0xFF00, "pure", "response: \\n%{public}s", getResponseStr.c\_str());
        }
    
      _  // ..._
    }
    

结果展示

终端使用curl指令获取的网站信息一致。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0/v3/L-I6iwFmRq-P9MCf9FBsQw/zh-cn_image_0000002624635800.png?HW-CC-KV=V1&HW-CC-Date=20260709T133831Z&HW-CC-Expire=86400&HW-CC-Sign=CF0D3BC7C66C360F517F9A1AA2A078F3BA3153E55777BBEA52694D5D2370C604 "点击放大")

**参考链接**

[使用命令行CMake构建NDK工程](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/build-with-ndk-cmake)

[使用lycium工具快速编译三方库](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/toolchain-lycium-build-project)