2

我正在尝试使用 reqwest 执行 HTTP GET 请求并将响应正文打印到 STDOUT。这适用于大多数网站,但它会为 amazon.com 返回奇怪的二进制输出:

#[tokio::main]
async fn main() {
    run().await;
}

async fn run() {
    let url = "https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/";
    let resp = reqwest::get(url).await.unwrap();
    let text = resp.text().await.unwrap();
    println!("{}", text);
}

为什么会resp.text().await.unwrap()返回二进制数据,如何从中获取正常的 HTTP 正文?

curl 返回我期望的 HTML:

curl https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/
4

1 回答 1

4

如果你这样做curl https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/ - I,你会看到:

server: Server
content-type: text/html
content-length: 2148
content-encoding: gzip
x-amz-rid: 2T9PBCY66S79SMC424V2
vary: Accept-Encoding
akamai-cache-status: Miss
date: Sat, 29 Feb 2020 22:23:54 GMT

content-encoding: gzip你需要做什么很明显。gzip从 reqwest结帐。gzip是一个可选功能,请参阅cargo doc,对于 reqwest 你可以写reqwest = { version = "0.10.3", features = ["gzip"] }在你的Cargo.toml.

于 2020-02-29T22:25:07.833 回答