reqwest 是一个简单而强大的 RUST HTTP 客户端,用于浏览器异步 HTTP 请求。支持 xmlHttpRequest, JSONP, CORS, 和 CommonJS 约束。
[dependencies]
reqwest = { version = "0.11.11", features = ["json", "cookies"] }
tokio = { version = "1", features = ["full"] }
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use reqwest::{cookie::Jar, Url};
use tokio;
#[tokio::main]
async fn main() {
let data = request_data().await.unwrap();
println!("{:?}", data);
}
async fn request_data() -> Result<String, Box<dyn Error>> {
let mut map = HashMap::new();
map.insert("q", "设置");
map.insert("page", "1");
map.insert("pageSize", "10");
map.insert("ctoken", "69_e6Yu_Zr_G8bLWy1KQNxaK");
println!("map: {:?}", map);
// 设置cookie
let cookie = "ctoken=69_e6Yu_Zr_G8bLWy1KQNxaK";
let url = "https://www.iconfont.cn".parse::<Url>().unwrap();
let jar = Jar::default();
jar.add_cookie_str(cookie, &url);
let client = reqwest::Client::builder()
.cookie_provider(Arc::new(jar))
.build()
.unwrap();
let res = client
.post("https://www.iconfont.cn/api/icon/search.json")
.json(&map)
.send()
.await?
.text()
.await?;
Ok(res)
}