如何在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/90/v3/XMiFQS1lSfas57eFdQpS8A/zh-cn_image_0000002194158760.png?HW-CC-KV=V1&HW-CC-Date=20260612T074657Z&HW-CC-Expire=86400&HW-CC-Sign=502B6E5171AB9F2BD935CF59600147B90AC030695ED4A8DD63C7E17C5510F4D4 "点击放大")
    
2.  在CMakeLists.txt文件中链接Curl对应的so库。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/b0/v3/ml-wmUDtRm-M56DdxtbKsA/zh-cn_image_0000002194158764.png?HW-CC-KV=V1&HW-CC-Date=20260612T074657Z&HW-CC-Expire=86400&HW-CC-Sign=65FE6D7E7A7A441DA11A833A5B3490A328B254766FDDE66B5545353E74A2448B "点击放大")
    
3.  在Native侧的.cpp文件中引入头文件curl.h，使用Curl的相关能力。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/84/v3/7OM0lnJeTeidMDcsA9R5sQ/zh-cn_image_0000002229758629.png?HW-CC-KV=V1&HW-CC-Date=20260612T074657Z&HW-CC-Expire=86400&HW-CC-Sign=EC4DD48568AB4988DF4365FE2858BFEDB1FAB99423422AB8B9149C06A3F72181 "点击放大")
    
    具体可参考以下代码：
    
    #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/72/v3/tmo_LpfERKqhwlF9ue7COA/zh-cn_image_0000002229604137.png?HW-CC-KV=V1&HW-CC-Date=20260612T074657Z&HW-CC-Expire=86400&HW-CC-Sign=11F3A1BE38C3A2402B7B6A6938681A4A2C1231C4DCC96D3E53C5067F90FD8BCA "点击放大")

**参考链接**

[使用命令行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)