i am trying to do an http request, and read the response line by line, this is my piece of code
use std::time::Instant;
use std::error::Error;
use url::Url;
use futures::join;
use console::style;
use tokio::net::TcpStream;
use tokio::io::AsyncWriteExt;
use tokio::io::AsyncBufReadExt;
async fn len(url: &str) {
let url = Url::parse(url).unwrap();
let host = url.host_str().unwrap();
let path = url.path();
let mut stream = TcpStream::connect(format!("{}:80", host).as_str()).await.unwrap();
let req = vec![
format!("GET {} HTTP/1.1", path).as_str(),
format!("Host: {}", host).as_str(),
"\r\n",
].join("\r\n");
stream.write_all(req.as_bytes()).await.unwrap();
let line = String::new();
stream.read_line(&mut line).await.unwrap();
println!("{}", line);
}
and this is tokio in Cargo.toml
tokio = { version = "0.2.11", features = ["time", "macros", "tcp", "rt-threaded", "dns", "io-util"] }
when i run, i get this error
error[E0599]: no method named `read_line` found for type `tokio::net::tcp::stream::TcpStream` in the current scope
--> src/main.rs:26:12
|
26 | stream.read_line(&mut line).await.unwrap();
| ^^^^^^^^^ method not found in `tokio::net::tcp::stream::TcpStream`
|
= note: the method `read_line` exists but the following trait bounds were not satisfied:
`tokio::net::tcp::stream::TcpStream : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
error: could not compile `gutenberg`.
To learn more, run the command again with --verbose.