为什么 reqwest HTTP 库返回二进制数据而不是文本正文?

2024-03-12

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

如果你这样做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 https://docs.rs/reqwest/0.10.3/reqwest/struct.ClientBuilder.html#method.gzip来自请求韦斯特。gzip is a 可选功能 https://docs.rs/reqwest/0.10.3/reqwest/index.html#optional-features, see 货物文件 https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section,对于 reqwest 你可以写reqwest = { version = "0.10.3", features = ["gzip"] }在你的Cargo.toml.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 reqwest HTTP 库返回二进制数据而不是文本正文? 的相关文章

随机推荐