使用 libcurl 在native层发起网络请求
1.文档
1.1 curl官方文档
curl 官网 | https://curl.se/ |
curl 代码 | https://github.com/curl/curl |
编译android so |
https://curl.se/docs/install.html Android小节 https://github.com/curl/curl/blob/c7bc689/docs/INSTALL.md#android |
api 示例 | https://curl.se/libcurl/c/example.html |
api 完整版 | https://curl.se/libcurl/c/ |
api 快速入门 | https://curl.se/libcurl/c/libcurl-easy.html |
1.2 android相关文档
android ndk-build命令 | https://developer.android.google.cn/ndk/guides/ndk-build?hl=zh-cn |
android 用ndk编译其它源码 | https://developer.android.google.cn/ndk/guides/other_build_systems |
移植教程 | https://gist.github.com/bertrandmartel/d964444f4a85c2598053 |
android 示例 (libcurl + openssl) | https://github.com/robertying/AndroidCurlExample |
2.编译openssl
https://www.cnblogs.com/sjjg/p/14173997.html
把生成的 libssl.a 和 libcrypto.a 复制到 android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/ 下对应的cpu架构根目录里。
把openssl include目录下的 crypto internal openssl 3个目录复制到 android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/ 目录里。
3.ndk-build编译libcurl库
3.1.检查3个工具
如不存在则安装
$sudo apt-get install automake autoconf libtool
3.2 运行autoconf
$autoreconf -vif
3.3 设置编译环境
新建一个setenv.sh 然后刷新:
export NDK=~/android/sdk/ndk/22.1.7171670 export PATH=$ANDROID:$PATH:$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin:$NDK/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin:. #curl x86_64 export HOST_TAG="linux-x86_64" export TARGET="x86_64-linux-android" export API=30 export TOOLCHAIN=${NDK}/toolchains/llvm/prebuilt/${HOST_TAG} export AR=${TOOLCHAIN}/bin/${TARGET}-ar export AS=${TOOLCHAIN}/bin/${TARGET}-as export CC=${TOOLCHAIN}/bin/${TARGET}${API}-clang export CXX=${TOOLCHAIN}/bin/${TARGET}${API}-clang++ export LD=${TOOLCHAIN}/bin/ld if [ "$TARGET" == "armv7a-linux-androideabi" ] ;then export RANLIB=${TOOLCHAIN}/bin/arm-linux-androideabi-ranlib export STRIP=${TOOLCHAIN}/bin/arm-linux-androideabi-strip else export RANLIB=${TOOLCHAIN}/bin/${TARGET}-ranlib export STRIP=${TOOLCHAIN}/bin/${TARGET}-strip fi
其中:
- as :汇编器
- ld :链接器或加载器
- ar :静态库打包器
- ranlib :为 ar 打包的文件创建索引
- strip :经常用来去除目标文件中的一些符号表、调试符号表信息,以减小程序的大小
TOOLCHAIN只能是如下两个之一 |
|
TARGET可选的值如下 |
|
官方文档中的问题 |
LD不要用以下这几种写法,否则无法生成.so |
3.4 配置参数
$./configure --enable-ipv6 --enable-http --enable-tftp --enable-ftp --disable-ldap --disable-ldaps --disable-rtsp --disable-dict --disable-telnet --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-manual --enable-file --without-zlib --without-ca-bundle --without-ca-path --without-ssl --enable-optimize --host=$TARGET
其中 --with-openssl="$TOOLCHAIN/sysroot/usr" 开启openssl
./configure --help 可查看有哪些参数。
3.5 make
$make -j8 V=1
使用V=1输出详细信息
3.6 生成的文件
4.示例
4.1 https
1 #define URL "https://mat1.gtimg.com/pingjs/ext2020/qqindex2018/dist/img/qq_logo_2x.png" 2 3 static void 4 test_easy_curl(const char *cert){ 5 CURL *curl; 6 CURLcode res; 7 char error[1024]; 8 9 curl = curl_easy_init(); 10 curl_easy_setopt(curl, CURLOPT_URL, URL); 11 #ifdef __ANDROID__ 12 curl_easy_setopt(curl, CURLOPT_CAINFO, cert); 13 #endif 14 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); 15 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true); 16 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER , error ); 17 18 res = curl_easy_perform(curl); 19 LOGE("result = %d, err = %s",res,error); 20 curl_easy_cleanup(curl); 21 }
4.2 post
1 static void 2 test_easy_curl_post(const char *cert){ 3 CURL *curl; 4 CURLcode res; 5 char error[1024]; 6 7 curl = curl_easy_init(); 8 curl_easy_setopt(curl, CURLOPT_URL, URL); 9 #ifdef __ANDROID__ 10 curl_easy_setopt(curl, CURLOPT_CAINFO, cert); 11 #endif 12 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); 13 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true); 14 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER , error); 15 16 /* Now specify the POST data */ 17 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=liq&age=23"); 18 19 print_cookies(curl); 20 res = curl_easy_perform(curl); 21 LOGE("result = %d, err = %s", res, error); 22 curl_easy_cleanup(curl); 23 }
multi-post: https://curl.se/libcurl/c/multi-post.html
4.3 tcp/rtsp
tcp : https://curl.se/libcurl/c/externalsocket.html https://curl.se/libcurl/c/ghiper.html https://curl.se/libcurl/c/hiperfifo.html
rtsp: https://curl.se/libcurl/c/rtsp.html