Linux C++使用libcurl访问http编程

11 篇文章 0 订阅

curl 和libcurl 的区别

curl是命令行工具,可以通过shell或脚本来运行curl。curl底层所使用的库是libcurl。 
libcurl是一个库,通常与别的程序绑定在一起使用,如命令行工具curl就是封装了libcurl库,libcurl源代码编译完成后会生成curl的可执行文件

 

下载安装libcurl库

首先看下自己有没有安装curl,执行了下curl  www.baidu.com 发现居然可以
找了下这个curl,发现是python2.7里带的,这是个可执行文件,也就是一个命令行工具
但是我们要使用curl来编写程序还是得依靠libcurl,所以还是得下载安装libcurl 库

1.选择安装目录,cd   /usr/local

2.wget https://curl.haxx.se/download/curl-7.53.0.tar.gz

3.tar -vxzf curl-7.53.0.tar.gz 

4.cd   /usr/local/ curl-7.53.0

5.编译安装
  ./configure
  make
  make install

6.查看是否安装成功 curl --version,如图:

进入到默认安装的目录下 /usr/local/bin 中发现有两个可执行文件curl 以及 curl-config,说明安装成功

7.查找curl.h 
find  /   -name   curl.h

一般linux的库文件是放在/usr/lib下,而这个头文件是在curl/curl.h
所以用 #include "curl/curl.h" ,经测试,能找到
说明可能在安装过程中上面这两个目录之一被加到环境变量,或者库链接目录中了

 

编译测试libcurl

进入 /usr/local/curl 7.53.0/docs 目录,找到examples 文件夹,进入,其中README 文件详细介绍了 libcurl 示例代码的使用方式:

Most examples should build fine using a command line like this: 
$ curl-config --cc --cflags --libs -o example example.c 
Some compilers don’t like having the arguments in this order but instead want you do reorganize them like: 
$ curl-config --cc -o example example.c curl-config --cflags --libs

也就是说,只要我们在上一步中成功安装了 curl 以及 curl-config 工具,在这一步中,我们只需要简单的运行这行指令即可自动的指定代码的包含头文件以及库文件信息:

$ `curl-config --cc` -o example example.c `curl-config --cflags --libs`

编译一下 examples 中的 https.c 文件,执行如下命令,如图:
 `curl-config --cc` -o https https.c `curl-config --cflags --libs`

就可以看到编译成功了https.c,然后可以看到有一个https的可执行文件

执行  ./https

可以看到成功返回可一个html 的内容,然后我们可以借鉴https.c来编写自己的C/C++程序了

https.c:

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* <DESC>
 * Simple HTTPS GET
 * </DESC>
 */
#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://example.com/");

#ifdef SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who isn't using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif

#ifdef SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you're connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif

    /* 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;
}

 

 

参考:https://blog.csdn.net/u012814856/article/details/81638421

 

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值