【问题标题】:Displaying the response body with Hyper only shows the size of the body使用 Hyper 显示响应正文仅显示正文的大小
【发布时间】:2016-11-04 00:40:39
【问题描述】:

我尝试使用 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

我做错了什么?我是不是误会了什么?

【问题讨论】:

    标签: rust hyper


    【解决方案1】:

    您正在将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);
    
    }
    

    【讨论】:

    • "hyper = 012.14" 呢?
    【解决方案2】:

    这里是如何使用 tokio 0.2、hyper 0.13 和 async/await 语法打印响应状态和正文。

    use std::error::Error;
    
    use hyper::body;
    use hyper::{Body, Client, Response};
    use hyper_tls::HttpsConnector;
    use tokio;
    
    #[tokio::main]
    async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);
    
        let res = client
            .get("https://www.reddit.com/r/programming/.rss".parse().unwrap())
            .await?;
    
        println!("Status: {}", res.status());
    
        let body_bytes = body::to_bytes(res.into_body()).await?;
        let body = String::from_utf8(body_bytes.to_vec()).expect("response was not valid utf-8");
        println!("Body: {}", body);
    
        Ok(())
    }
    

    【讨论】:

      【解决方案3】:

      从 hyper 0.12 开始,只要网页是有效的 UTF-8,以下工作:

      extern crate hyper;
      extern crate hyper_tls;
      
      use hyper::Client;
      use hyper::rt::{self, Future, Stream};
      use hyper_tls::HttpsConnector;
      
      fn main() {
          rt::run(rt::lazy(|| {
              let https = HttpsConnector::new(4).unwrap();
              let client = Client::builder().build::<_, hyper::Body>(https);
      
              client.get("https://www.reddit.com/r/programming/.rss".parse().unwrap())
                  .and_then(|res| {
                      println!("status {}", res.status());
                      res.into_body().concat2()
                  }).map(|body| {
                      println!("Body {}", String::from_utf8(body.to_vec()).unwrap());
                  })
                  .map_err(|err| {
                      println!("error {}", err)
                  })
          }));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 2019-05-18
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        相关资源
        最近更新 更多