Rust ureq
ureq 是 Rust 生态中一个简单、安全的 HTTP 客户端库主打同步阻塞 I/O以极简的 API 和最小的依赖树为核心设计理念。核心特点- 纯 Rust 实现禁止使用 unsafe 代码安全性高- 同步阻塞 I/O不使用 async/awaitAPI 简单直观依赖极少- 链式调用支持类似 Builder 模式的链式构建请求- 功能丰富支持 cookies、JSON、HTTP 代理、HTTPS、字符集解码等- TLS 支持可选 rustls 或 native-tls- 连接池复用通过 Agent 管理连接池和 cookie 状态安装配置在 Cargo.toml 中添加依赖[dependencies]ureq 2.9serde 1.0serde_json 1.0如需 JSON 支持启用 featureureq { version 2.9, features [json] }基本用法GET 请求fn main() - Result {let body: String ureq::get(http://example.com).set(User-Agent, Mozilla/5.0).call()?.into_string()?;println!({}, body);Ok(())}POST JSON 请求use serde_json::json;fn main() - Result {let resp ureq::post(https://myapi.example.com/ingest).set(Content-Type, application/json).send_json(json!({name: martin,rust: true}))?;if resp.ok() {let body resp.into_string()?;println!({}, body);}Ok(())}使用 Agent连接池 超时配置use ureq::Agent;use std::time::Duration;fn main() - Result {let agent: Agent ureq::AgentBuilder::new().timeout_read(Duration::from_secs(5)).timeout_write(Duration::from_secs(5)).build();let body: String agent.get(http://example.com/page).call()?.into_string()?;// 复用连接池中的连接let response: String agent.put(http://example.com/upload).set(Authorization, Bearer token).call()?.into_string()?;Ok(())}常用 API 一览方法 说明ureq::get(url) 发起 GET 请求ureq::post(url) 发起 POST 请求.set(key, value) 设置请求头.call() 发送无 body 的请求.send_string(s) 发送字符串 body.send_json(json) 发送 JSON body需启用 json feature.into_string() 将响应体转为 String.into_json() 将响应体反序列化为 JSON需启用 json featureureq::agent() 创建 Agent 管理连接池和 cookiesureq vs reqwest对比项 ureq reqwestI/O 模型 同步阻塞 异步tokio依赖量 极少 较多API 风格 简洁直观 功能全面适用场景 脚本、CLI 工具、简单 API 调用 高并发 Web 服务如果你不需要异步追求简单和低依赖ureq 是非常好的选择。需要我帮你写一个带代理和重试机制的完整示例吗