看看Hyper.
发送GET请求就这么简单.
let client = Client::new();
let res = client.get("http://example.domain").send().unwrap();
assert_eq!(res.status, hyper::Ok);
你可以在documentation页中找到更多的例子.
Edit:
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;
fn main() {
let mut core = Core::new().unwrap();
let client = Client::new(&core.handle());
let uri = "http://httpbin.org/ip".parse().unwrap();
let work =
client.get(uri).and_then(|res| {
println!("Response: {}", res.status());
res.body().for_each(|chunk| {
io::stdout()
.write_all(&chunk)
.map_err(From::from)
})
});
core.run(work).unwrap();
}
以下是所需的依赖项.
[dependencies]
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"