使用 ASIO 库创建 HTTP GET 请求并接收响应数据 - 示例指南

本文将提供一个使用 ASIO 库创建 HTTP GET 请求并接收响应数据的示例代码。代码示例将请求 http://fangke.zstu.edu.cn:6007/swagger,并打印返回的数据内容。

代码示例

#include <iostream>
#include <asio.hpp>

using asio::ip::tcp;

int main() {
    try {
        asio::io_context io_context;
        
        // 1. 解析主机名和端口号
        tcp::resolver resolver(io_context);
        tcp::resolver::results_type endpoints = resolver.resolve('fangke.zstu.edu.cn', '6007');

        // 2. 创建socket连接
        tcp::socket socket(io_context);
        asio::connect(socket, endpoints);

        // 3. 发送GET请求
        asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << 'GET /swagger HTTP/1.1\r\n';
        request_stream << 'Host: fangke.zstu.edu.cn:6007\r\n';
        request_stream << 'Connection: close\r\n\r\n';

        asio::write(socket, request);

        // 4. 接收响应
        asio::streambuf response;
        asio::read_until(socket, response, '\r\n');

        // 5. 输出响应头
        std::istream response_stream(&response);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != 'HTTP/') {
            std::cout << 'Invalid response\n';
            return 1;
        }
        if (status_code != 200) {
            std::cout << 'Response returned with status code ' << status_code << '\n';
            return 1;
        }
        std::cout << 'Response status code: ' << status_code << '\n';

        // 6. 输出响应正文
        asio::read_until(socket, response, '\r\n\r\n');
        std::string header;
        while (std::getline(response_stream, header) && header != '\r') {}
        std::cout << '\n';
        if (response.size() > 0) {
            std::cout << &response;
        }

        return 0;
    }
    catch (std::exception& e) {
        std::cout << 'Exception: ' << e.what() << '\n';
        return 1;
    }
}

代码解释

这段代码使用了 ASIO 库中的 tcp::resolver, tcp::socket, asio::streambuf 等类以及相应的成员函数来完成 GET 请求的发送和响应的接收。

  1. 解析主机名和端口号:使用 tcp::resolver 类解析指定主机名和端口号,得到 tcp::resolver::results_type 类型的 endpoint 信息。
  2. 创建 socket 连接:使用 tcp::socket 类创建 socket 连接,并使用 asio::connect 函数连接到解析得到的 endpoint。
  3. 发送 GET 请求:使用 asio::streambuf 对象存储 GET 请求数据,使用 std::ostream 对象将请求数据写入 asio::streambuf,然后使用 asio::write 函数发送请求到服务器。
  4. 接收响应:使用 asio::read_until 函数从服务器接收响应数据,直到遇到 '\r\n' 字符。
  5. 解析响应头:使用 std::istream 对象从 asio::streambuf 读取响应数据,并解析 HTTP 版本、状态码和状态信息。
  6. 输出响应正文:使用 asio::read_until 函数继续接收响应数据,直到遇到 '\r\n\r\n' 字符,然后将响应正文打印到控制台。

代码使用说明

  1. 确保已安装 ASIO 库,并包含 <asio.hpp> 头文件。
  2. 将代码中的 'fangke.zstu.edu.cn' 和 '6007' 替换为需要访问的网站的主机名和端口号。
  3. 编译并运行代码。

其他说明

  • 代码示例仅用于演示 ASIO 库的使用,并非完整的 HTTP 客户端实现。
  • 实际开发中需要根据具体需求进行修改和完善。
  • 可以参考 ASIO 库的官方文档了解更多信息。
使用 ASIO 库创建 HTTP GET 请求并接收响应数据 - 示例指南

原文地址: https://www.cveoy.top/t/topic/m18L 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录