2016-07-01 8 views
5

starałem się wyświetlać zawartość (korpus) z adresu URL jako tekst za pomocą HyperWyświetlanie treść odpowiedzi z Hyper pokazuje tylko rozmiar ciała

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); 

} 

Ale działa ten skrypt tylko zwraca rozmiar ciała :

Result: 22871 

Co zrobiłem źle? Czy coś źle zrozumiałem?

Odpowiedz

10

Czytasz wynik get w s, ale drukujesz wynik tej funkcji, która jest liczbą odczytanych bajtów. See the documentation for Read::read_to_string.

Zatem kod który drukuje pobrana treść jest:

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); 

} 
+0

haha ​​oops! Nie przeczytałem ponownie mojego kodu! –

Powiązane problemy