【问题标题】:How to use HTTP proxy with asynchronous Hyper 0.11 in Rust?如何在 Rust 中使用 HTTP 代理和异步 Hyper 0.11?
【发布时间】:2018-01-01 23:56:59
【问题描述】:

如何使用 Hyper 0.11 的代理发送 HTTP 请求?我有以下工作代码,可以在没有代理的情况下发送 HTTP 请求:

extern crate hyper;
extern crate tokio_core;
extern crate futures;

use futures::Future;
use hyper::Client;
use tokio_core::reactor::Core;

fn main() {
    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle());

    let uri = "http://stackoverflow.com".parse().unwrap();
    let work = client.get(uri).map(|res| {
        res.status()
    });

    match core.run(work) {
        Ok(status) => println!("Status: {}", status),
        Err(e) => println!("Error: {:?}", e)
    }
}

这不是 How to reach an HTTPS site via proxy with Hyper? 的重复,因为我询问的是 Hyper 0.11 的新版本,它具有与以前版本不兼容的完全不同的 API。

【问题讨论】:

标签: http proxy rust hyper


【解决方案1】:

你可以使用Client::configure()方法构建一个代理连接器,然后用handle构建它,见下面代码sn-p

let handle = core.handle();
let proxy = {
    let proxy_uri ="http://<your proxy>:port".parse().unwrap();
    let mut proxy = Proxy::new(Intercept::All, proxy_uri);
    proxy.set_authorization(Basic{
        username: "your username",
        password: Some("your passwd"),
    }); 
    let http_connector = HttpConnector::new(4, &handle);
    ProxyConnector::from_proxy(http_connector, proxy).unwrap()
};
let client = Client::configure().connector(proxy).build(&handle)

现在您可以使用客户端对象来执行任何 REST 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2019-10-30
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多