http - 为什么 reqwest HTTP 库返回二进制数据而不是文本正文?
问题描述
我正在尝试使用 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
从 reqwest结帐。gzip
是一个可选功能,请参阅cargo doc,对于 reqwest 你可以写reqwest = { version = "0.10.3", features = ["gzip"] }
在你的Cargo.toml
.
推荐阅读
- azure - 如何使用 Power shell 将 Web 作业自动部署到 Azure 门户
- elasticsearch - 在 Elasticsearch 中获取缺失值
- python-3.x - 什么原因导致“进入 DLASCLS 参数编号 4 时具有非法值”错误,我该如何解决?
- c# - 属性“背景”不支持 xamarin 表单中的“框架”类型错误值
- string - 将 esp 指向字符数组
- kubernetes - 如何使用 yml 文件在 K8S(Minikube)上公开一个简单的 rabbitmq 部署?
- core-data - 带有 CoreData 的 TextField
- oracle - 在 Oracle Analytics Server 5.5.0 中导入 .dva 文件时出现未知错误
- c# - C# 动态数据解析为 DateTime
- java - 在java中解密pdf文件时出错