Linux C/C++实现https post/get请求

Linux C/C++要实现https访问借助于libcurl工具,如果是实现自定义SSL证书通信校验的,可以通过C调用openssl来实现,也可以通过libcurl来。记得linux上提前安装好openssl库。

1.编译安装libcurl

curl库的代码下载地址:https://curl.haxx.se/download.html

第一步:进入curl工程目录执行./buidconf产生configure配置文件;

第二步:执行产生的configure脚本: ./configure

第三步:make

第四步:sudo make install

2.到/usr/local/lib/即可查看到安装好的库文件

<s /usr/local/lib250;48;5                          
libcurl.a   libcurl.so    libcurl.so.4.5.0  python2.7  python3.6
libcurl.la  libcurl.so.4  pkgconfig         python3.5

3.编写https代码:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();

  return 0;
}

4.编译运行:

gcc https.c -l curl -o https

./https

5.libcurl还可以更多的功能,具体可以参考

https://curl.haxx.se/libcurl/c/example.html

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值