【Hyper】发送 GET 请求

环境

  • Time 2022-01-15
  • Rust 1.58.0
  • Tokio 1.15.0
  • Hyper 0.14.16

概念

参考:https://hyper.rs/guides/client/basic/

示例

main.rs

use std::error::Error;

use hyper::{body::HttpBody, Client};
use tokio::io::{stdout, AsyncWriteExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let client = Client::new();
    let uri = "http://127.0.0.1:4444".parse()?;
    let mut resp = client.get(uri).await?;
    println!("Response: {}", resp.status());

    while let Some(chunk) = resp.body_mut().data().await {
        stdout().write_all(&chunk?).await?;
    }

    Ok(())
}

输出

使用客户端连接编写的 HTTP 服务器。

Response: 200 OK
Hello World

总结

使用 Hyper 发送 GET 请求。

附录

posted @   jiangbo4444  阅读(130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-04-27 Spring cloud:网关-Zuul路由
点击右上角即可分享
微信分享提示