借助curl实现C++客户端请求

最近有个需求是C++作为客户端请求服务器,获取返回结果,查找了一些库C++网络库,其中mongoose只有一个.h头文件和一个.c源文件,比较方便且轻量化,客户端上传文件可以跑通,但是上传json数据时需要自己编写报文头,自己不太懂网络这一块,所以最终采用了curl库。

一、curl库编译安装

curl功能比较完善,可以根据自身功能需求进行编译,我这里只需要作为客户端请求服务端,所以编译的时候去掉了很多东西,这样作为第三方库依赖也会相应减少。

linux编译

  • step1
$ ./configure --prefix=/root/package/curl_build --disable-shared --disable-thread --without-ssl --disable-ldap --disable-ldaps --without-zlib
  • step2
$ make
  • step3
$ make install

在/root/package/curl_build目录下的bin文件中,运行./curl --version查看编译后的库支持的功能,
运行./curl-config --static-libs或者ldd curl查看依赖的库文件

windows编译

  • step1
$ cd curl-7.70.0/winbuild
  • step2
    select vs2019 x64 Native Tools Command Prompt for vs 2019 with manager(管理员身份打开)
nmake /f Makefile.vc mode=static VC=15 MACHINE=x64 DEBUG=no ENABLE_WINSSL=no ENABLE_IDN=no ENABLE_SSPI=no
  • step3
    select libcurl-vc15-x64-release-static-ipv6-sspi-winssl

在bin目录下,运行./curl.exe --version查看编译后的库支持的功能,
利用Dependency Walker工具查看依赖的库文件

二、代码实现

详细代码见github

参考资料

https://curl.se/docs/install.html
https://blog.csdn.net/px41834/article/details/81627170
https://blog.csdn.net/cym1990/article/details/79851039
https://my.oschina.net/u/4324616/blog/4326368
https://www.jianshu.com/p/eef34d40f51c
https://blog.csdn.net/u014664846/article/details/91411997
https://blog.csdn.net/sinat_29891353/article/details/72526173
https://blog.csdn.net/qq_41482046/article/details/93379733
https://blog.csdn.net/giveaname/article/details/107388468

原文地址:https://www.cnblogs.com/xiaxuexiaoab/p/14578904.html