Hyper在HTTPS URL中显示“无效的Http方案”。

12

我有

hyper = "0.10"

而下面的代码:

let client = Client::new();
let mut res = client.get("https://google.com").send().unwrap();

在Rust中,我收到了一个错误信息,好像它没有SSL支持:

无效的Http协议

这是在Debian jessie上的Rust 1.14.0版本。

如何让Hyper通过SSL连接到HTTPS URL?


你能展示完整的错误信息吗? - E net4
你可能想查看这个答案,它使用了新的 hyper-native-tls crate。链接 - squiguy
对于 Hyper 0.11 版本,答案在文档的“客户端配置”部分中:https://hyper.rs/guides/client/configuration/。 - steamer25
1个回答

19

试试这个:

extern crate hyper;
extern crate hyper_native_tls;

use hyper::Client;
use hyper::net::HttpsConnector;
use hyper_native_tls::NativeTlsClient;

fn main() {
    let ssl = NativeTlsClient::new().unwrap();
    let connector = HttpsConnector::new(ssl);
    let client = Client::with_connector(connector);
    let mut res = client.get("https://google.com").send().unwrap();
}

主要参考这个答案。缺失的是 Client::with_connector 部分。


有趣的是,hyper 在允许使用 https 方案之前确保你拥有能够处理 TLS 的客户端。这很棒! - Matthieu M.
1
@MatthieuM。我相信这是最近的更改,因为TLS相关的代码被放入了其他的crate中。这段代码正在使用hyper 0.10。 - squiguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接