linux c http curl,Linux C++使用libcurl访问http编程

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, , 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.

*

***************************************************************************/

/*

* Simple HTTPS GET

*

*/

#include

#include

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

---------------------

作者:Bird鸟人

来源:CSDN

原文:https://blog.csdn.net/wcc27857285/article/details/86529034

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux下进行HTTP编程,可以使用C++的网络编程库如libcurl、Boost.Asio等。 使用libcurl实现HTTP请求的步骤如下: 1. 初始化curl: `curl_global_init(CURL_GLOBAL_DEFAULT)` 2. 得到一个`CURL`句柄: `CURL *curl = curl_easy_init()` 3. 设置HTTP请求的URL: `curl_easy_setopt(curl, CURLOPT_URL, "http://example.com")` 4. 执行请求: `curl_easy_perform(curl)` 5. 清理curl: `curl_easy_cleanup(curl)` 下面是一个使用libcurl实现HTTP GET请求的示例代码: ```c++ #include <iostream> #include <curl/curl.h> int main() { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } ``` 如果需要进行HTTP POST请求,可以使用`curl_easy_setopt()`函数设置`CURLOPT_POST`选项为`true`,并使用`CURLOPT_POSTFIELDS`选项设置POST请求的数据。下面是一个使用libcurl实现HTTP POST请求的示例代码: ```c++ #include <iostream> #include <curl/curl.h> int main() { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data1=value1&data2=value2"); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } ``` 使用Boost.Asio实现HTTP请求的步骤如下: 1. 创建`boost::asio::io_context`对象: `boost::asio::io_context io_context;` 2. 创建`boost::asio::ip::tcp::resolver`对象,用于解析URL: `boost::asio::ip::tcp::resolver resolver(io_context);` 3. 解析URL,得到服务器的IP地址和端口: `auto endpoints = resolver.resolve("example.com", "http");` 4. 创建`boost::asio::ip::tcp::socket`对象,并连接到服务器: `boost::asio::ip::tcp::socket socket(io_context); socket.connect(*endpoints);` 5. 构造HTTP请求,发送请求: `boost::asio::write(socket, boost::asio::buffer("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"));` 6. 读取HTTP响应: `boost::asio::streambuf response; boost::asio::read_until(socket, response, "\r\n");` 7. 清理socket: `socket.close();` 下面是一个使用Boost.Asio实现HTTP GET请求的示例代码: ```c++ #include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_context io_context; boost::asio::ip::tcp::resolver resolver(io_context); auto endpoints = resolver.resolve("example.com", "http"); boost::asio::ip::tcp::socket socket(io_context); boost::asio::connect(socket, endpoints); boost::asio::write(socket, boost::asio::buffer("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")); boost::asio::streambuf response; boost::asio::read_until(socket, response, "\r\n"); std::cout << &response; socket.close(); return 0; } ``` 如果需要进行HTTP POST请求,可以将POST请求的数据放在HTTP请求的正文中,并设置`Content-Length`头部。下面是一个使用Boost.Asio实现HTTP POST请求的示例代码: ```c++ #include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_context io_context; boost::asio::ip::tcp::resolver resolver(io_context); auto endpoints = resolver.resolve("example.com", "http"); boost::asio::ip::tcp::socket socket(io_context); boost::asio::connect(socket, endpoints); std::string post_data = "data1=value1&data2=value2"; std::string request = "POST / HTTP/1.1\r\n" "Host: example.com\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: " + std::to_string(post_data.length()) + "\r\n" "\r\n" + post_data; boost::asio::write(socket, boost::asio::buffer(request)); boost::asio::streambuf response; boost::asio::read_until(socket, response, "\r\n"); std::cout << &response; socket.close(); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值