【问题标题】:How can I accept invalid or self-signed SSL certificates in Rust futures reqwest?如何在 Rust 期货 reqwest 中接受无效或自签名 SSL 证书?
【发布时间】:2021-05-04 17:13:59
【问题描述】:

我的代码如下所示:

let fetches = futures::stream::iter(
    hosts.into_iter().map(|url| {
        async move {
                match reqwest::get(&url).await {
                    // Ok and Err statements here!
                }

但是,这里的问题是它会为带有无效或自签名 SSL 证书的 URL 提供错误。因此,我尝试执行以下操作:

let fetches = futures::stream::iter(
    hosts.into_iter().map(|url| {
        async move {
            match reqwest::Client::builder().danger_accept_invalid_certs(true).build().unwrap().get(&url).await {
                // Ok and Err statements here!
            }

当我尝试使用 Cargo 构建它时,它显示“error[E0277]: `RequestBuilder` is not a future”。

那么,如何让我的代码接受无效证书?

【问题讨论】:

    标签: rust rust-cargo rust-tokio reqwest


    【解决方案1】:

    与返回Response 的顶级get() 函数不同,您在第二个代码中调用的Client::get() 方法返回RequestBuilder,您必须send() 才能实际通信。

    p>

    添加缺少的send() 允许代码编译(playgropund):

    fn main() {
        let hosts: Vec<String> = vec![];
        let fetches = futures::stream::iter(hosts.into_iter().map(|url| async move {
            match reqwest::Client::builder()
                .danger_accept_invalid_certs(true)
                .build()
                .unwrap()
                .get(&url)
                .send()
                .await
            {
                Ok(x) => x,
                Err(x) => panic!(),
            }
        }));
    }
    

    【讨论】:

    • 感谢您的回答!它在编译代码时对我有用。但是,我仍然无法解决接受带有无效或自签名 SSL 证书的 URL 的主要问题。对此有什么想法吗?
    • @BinitGhimire 我希望 accept_invalid_certs 解决这个问题。如果没有,您可能想在project issue tracker 提出问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2011-06-26
    • 2019-11-04
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多