最近在做一个项目,需要用到http get post等

需求分析需要做到同步和异步,异步请求的返回以可选的回调通知的方式进行。

本人以Linux为例,一步一步的来实现。

  1. 配置并且编译libcurl
    我以在Linux底下的交叉编译举例。
    libcurl源码下载: http://curl.haxx.se/download.html
    配置libcurl支持https和zlib压缩,必须需要openssl和zlib库
    openssl库源码下载: http://www.openssl.org/source/。下载1.02a以上的版本,避开心脏出血漏洞。
    zlib源码下载:http://www.zlib.net/。下载最新版本代码。
    新建文件夹carbon。源码解压至目录carbon。

    1.1 配置openssl并且编译
    配置和编译脚本:
    libcurl的封装,支持同步异步请求,支持多线程下载,支持https(z)
      1 #!/bin/bash
      2 # Cross-compile environment for Android on ARMv7 and x86
      3 #
      4 # Contents licensed under the terms of the OpenSSL license
      5 # http://www.openssl.org/source/license.html
      6 #
      7 # See http://wiki.openssl.org/index.php/FIPS_Library_and_Android
      8 #   and http://wiki.openssl.org/index.php/Android
      9 
     10 #####################################################################
     11 
     12 # Set ANDROID_NDK_ROOT to you NDK location. For example,
     13 # /opt/android-ndk-r8e or /opt/android-ndk-r9. This can be done in a
     14 # login script. If ANDROID_NDK_ROOT is not specified, the script will
     15 # try to pick it up with the value of _ANDROID_NDK_ROOT below. If
     16 # ANDROID_NDK_ROOT is set, then the value is ignored.
     17 # _ANDROID_NDK="android-ndk-r8e"
     18 #_ANDROID_NDK="android-ndk-r9"
     19 _ANDROID_NDK="android-ndk-r10"
     20 ANDROID_NDK_ROOT=$HOME/ndk/android-ndk-r10d
     21 # Set _ANDROID_EABI to the EABI you want to use. You can find the
     22 # list in $ANDROID_NDK_ROOT/toolchains. This value is always used.
     23 # _ANDROID_EABI="x86-4.6"
     24 # _ANDROID_EABI="arm-linux-androideabi-4.6"
     25 _ANDROID_EABI="arm-linux-androideabi-4.8"
     26 export ROOTDIR="${PWD}"
     27 
     28 # Set _ANDROID_ARCH to the architecture you are building for.
     29 # This value is always used.
     30 # _ANDROID_ARCH=arch-x86
     31 _ANDROID_ARCH=arch-arm
     32 
     33 # Set _ANDROID_API to the API you want to use. You should set it
     34 # to one of: android-14, android-9, android-8, android-14, android-5
     35 # android-4, or android-3. You can't set it to the latest (for
     36 # example, API-17) because the NDK does not supply the platform. At
     37 # Android 5.0, there will likely be another platform added (android-22?).
     38 # This value is always used.
     39 # _ANDROID_API="android-14"
     40 # _ANDROID_API="android-18"
     41 # _ANDROID_API="android-19"
     42 _ANDROID_API="android-5"
     43 
     44 #####################################################################
     45 
     46 # If the user did not specify the NDK location, try and pick it up.
     47 # We expect something like ANDROID_NDK_ROOT=/opt/android-ndk-r8e
     48 # or ANDROID_NDK_ROOT=/usr/local/android-ndk-r8e.
     49 
     50 if [ -z "$ANDROID_NDK_ROOT" ]; then
     51 
     52   _ANDROID_NDK_ROOT=""
     53   if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/usr/local/$_ANDROID_NDK" ]; then
     54     _ANDROID_NDK_ROOT="/usr/local/$_ANDROID_NDK"
     55   fi
     56 
     57   if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/opt/$_ANDROID_NDK" ]; then
     58     _ANDROID_NDK_ROOT="/opt/$_ANDROID_NDK"
     59   fi
     60 
     61   if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$HOME/$_ANDROID_NDK" ]; then
     62     _ANDROID_NDK_ROOT="$HOME/$_ANDROID_NDK"
     63   fi
     64 
     65   if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$PWD/$_ANDROID_NDK" ]; then
     66     _ANDROID_NDK_ROOT="$PWD/$_ANDROID_NDK"
     67   fi
     68 
     69   # If a path was set, then export it
     70   if [ ! -z "$_ANDROID_NDK_ROOT" ] && [ -d "$_ANDROID_NDK_ROOT" ]; then
     71     export ANDROID_NDK_ROOT="$_ANDROID_NDK_ROOT"
     72   fi
     73 fi
     74 
     75 # Error checking
     76 # ANDROID_NDK_ROOT should always be set by the user (even when not running this script)
     77 # http://groups.google.com/group/android-ndk/browse_thread/thread/a998e139aca71d77
     78 if [ -z "$ANDROID_NDK_ROOT" ] || [ ! -d "$ANDROID_NDK_ROOT" ]; then
     79   echo "Error: ANDROID_NDK_ROOT is not a valid path. Please edit this script."
     80   # echo "$ANDROID_NDK_ROOT"
     81   # exit 1
     82 fi
     83 
     84 # Error checking
     85 if [ ! -d "$ANDROID_NDK_ROOT/toolchains" ]; then
     86   echo "Error: ANDROID_NDK_ROOT/toolchains is not a valid path. Please edit this script."
     87   # echo "$ANDROID_NDK_ROOT/toolchains"
     88   # exit 1
     89 fi
     90 
     91 # Error checking
     92 if [ ! -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI" ]; then
     93   echo "Error: ANDROID_EABI is not a valid path. Please edit this script."
     94   # echo "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI"
     95   # exit 1
     96 fi
     97 
     98 #####################################################################
     99 
    100 # Based on ANDROID_NDK_ROOT, try and pick up the required toolchain. We expect something like:
    101 # /opt/android-ndk-r83/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin
    102 # Once we locate the toolchain, we add it to the PATH. Note: this is the 'hard way' of
    103 # doing things according to the NDK documentation for Ice Cream Sandwich.
    104 # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
    105 
    106 ANDROID_TOOLCHAIN=""
    107 for host in "linux-x86_64" "linux-x86" "darwin-x86_64" "darwin-x86"
    108 do
    109   if [ -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin" ]; then
    110     ANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin"
    111     break
    112   fi
    113 done
    114 
    115 # Error checking
    116 if [ -z "$ANDROID_TOOLCHAIN" ] || [ ! -d "$ANDROID_TOOLCHAIN" ]; then
    117   echo "Error: ANDROID_TOOLCHAIN is not valid. Please edit this script."
    118   # echo "$ANDROID_TOOLCHAIN"
    119   # exit 1
    120 fi
    121 
    122 case $_ANDROID_ARCH in
    123     arch-arm)      
    124       ANDROID_TOOLS="arm-linux-androideabi-gcc arm-linux-androideabi-ranlib arm-linux-androideabi-ld"
    125       ;;
    126     arch-x86)      
    127       ANDROID_TOOLS="i686-linux-android-gcc i686-linux-android-ranlib i686-linux-android-ld"
    128       ;;      
    129     *)
    130       echo "ERROR ERROR ERROR"
    131       ;;
    132 esac
    133 
    134 for tool in $ANDROID_TOOLS
    135 do
    136   # Error checking
    137   if [ ! -e "$ANDROID_TOOLCHAIN/$tool" ]; then
    138     echo "Error: Failed to find $tool. Please edit this script."
    139     # echo "$ANDROID_TOOLCHAIN/$tool"
    140     # exit 1
    141   fi
    142 done
    143 
    144 # Only modify/export PATH if ANDROID_TOOLCHAIN good
    145 if [ ! -z "$ANDROID_TOOLCHAIN" ]; then
    146   export ANDROID_TOOLCHAIN="$ANDROID_TOOLCHAIN"
    147   export PATH="$ANDROID_TOOLCHAIN":"$PATH"
    148 fi
    149 
    150 #####################################################################
    151 
    152 # For the Android SYSROOT. Can be used on the command line with --sysroot
    153 # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
    154 export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH"
    155 export SYSROOT="$ANDROID_SYSROOT"
    156 export NDK_SYSROOT="$ANDROID_SYSROOT"
    157 
    158 # Error checking
    159 if [ -z "$ANDROID_SYSROOT" ] || [ ! -d "$ANDROID_SYSROOT" ]; then
    160   echo "Error: ANDROID_SYSROOT is not valid. Please edit this script."
    161   # echo "$ANDROID_SYSROOT"
    162   # exit 1
    163 fi
    164 
    165 #####################################################################
    166 
    167 # If the user did not specify the FIPS_SIG location, try and pick it up
    168 # If the user specified a bad location, then try and pick it up too.
    169 if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then
    170 
    171   # Try and locate it
    172   _FIPS_SIG=""
    173   if [ -d "/usr/local/ssl/$_ANDROID_API" ]; then
    174     _FIPS_SIG=`find "/usr/local/ssl/$_ANDROID_API" -name incore`
    175   fi
    176 
    177   if [ ! -e "$_FIPS_SIG" ]; then
    178     _FIPS_SIG=`find $PWD -name incore`
    179   fi
    180 
    181   # If a path was set, then export it
    182   if [ ! -z "$_FIPS_SIG" ] && [ -e "$_FIPS_SIG" ]; then
    183     export FIPS_SIG="$_FIPS_SIG"
    184   fi
    185 fi
    186 
    187 # Error checking. Its OK to ignore this if you are *not* building for FIPS
    188 if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then
    189   echo "Error: FIPS_SIG does not specify incore module. Please edit this script."
    190   # echo "$FIPS_SIG"
    191   # exit 1
    192 fi
    193 
    194 #####################################################################
    195 
    196 # Most of these should be OK (MACHINE, SYSTEM, ARCH). RELEASE is ignored.
    197 export MACHINE=armv7
    198 export RELEASE=2.6.37
    199 export SYSTEM=android
    200 export ARCH=arm
    201 export CROSS_COMPILE="arm-linux-androideabi-"
    202 
    203 if [ "$_ANDROID_ARCH" == "arch-x86" ]; then
    204     export MACHINE=i686
    205     export RELEASE=2.6.37
    206     export SYSTEM=android
    207     export ARCH=x86
    208     export CROSS_COMPILE="i686-linux-android-"
    209 fi
    210 
    211 # For the Android toolchain
    212 # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
    213 export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH"
    214 export SYSROOT="$ANDROID_SYSROOT"
    215 export NDK_SYSROOT="$ANDROID_SYSROOT"
    216 export ANDROID_NDK_SYSROOT="$ANDROID_SYSROOT"
    217 export ANDROID_API="$_ANDROID_API"
    218 
    219 # CROSS_COMPILE and ANDROID_DEV are DFW (Don't Fiddle With). Its used by OpenSSL build system.
    220 # export CROSS_COMPILE="arm-linux-androideabi-"
    221 export ANDROID_DEV="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH/usr"
    222 export HOSTCC=gcc
    223 
    224 VERBOSE=1
    225 if [ ! -z "$VERBOSE" ] && [ "$VERBOSE" != "0" ]; then
    226   echo "ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT"
    227   echo "ANDROID_ARCH: $_ANDROID_ARCH"
    228   echo "ANDROID_EABI: $_ANDROID_EABI"
    229   echo "ANDROID_API: $ANDROID_API"
    230   echo "ANDROID_SYSROOT: $ANDROID_SYSROOT"
    231   echo "ANDROID_TOOLCHAIN: $ANDROID_TOOLCHAIN"
    232   echo "FIPS_SIG: $FIPS_SIG"
    233   echo "CROSS_COMPILE: $CROSS_COMPILE"
    234   echo "ANDROID_DEV: $ANDROID_DEV"
    235 fi
    236 
    237 cd openssl
    238 if [ $# -gt 0 ]; then
    239 perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org
    240 ./config -DOPENSSL_NO_HEARTBEATS no-shared no-ssl2 no-ssl3 no-comp no-hw no-engine --openssldir=${ROOTDIR}/build/openssl
    241 fi
    242 make depend
    243 make && make install
    libcurl的封装,支持同步异步请求,支持多线程下载,支持https(z)

相关文章:

  • 2022-12-23
  • 2021-09-01
  • 2021-08-26
  • 2022-12-23
  • 2021-11-11
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2021-09-18
  • 2021-06-11
相关资源
相似解决方案