我尝试使用 Hyper 将 URL 的内容(正文)显示为文本
extern crate hyper;
use hyper::client::Client;
use std::io::Read;
fn main () {
let client = Client::new();
let mut s = String::new();
let res = client.get("https://www.reddit.com/r/programming/.rss")
.send()
.unwrap()
.read_to_string(&mut s)
.unwrap();
println!("Result: {}", res);
}
但是运行这个脚本只会返回正文的大小:
Result: 22871
我做错了什么?我是不是误会了什么?
最佳答案
您正在将 get
的结果读取到 s
中,但您正在打印此函数的结果,即读取的字节数。 See the documentation for Read::read_to_string
.
因此打印检索到的内容的代码是:
extern crate hyper;
use hyper::client::Client;
use std::io::Read;
fn main () {
let client = Client::new();
let mut s = String::new();
let res = client.get("https://www.reddit.com/r/programming/.rss")
.send()
.unwrap()
.read_to_string(&mut s)
.unwrap();
println!("Result: {}", s);
}
关于rust - 用 Hyper 显示响应体只显示响应体的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148163/