如何将HTTP代理与Rust中的异步Hyper 0.11一起使用?
如何使用带有Hyper 0.11的代理发送HTTP请求?我有以下不带代理发送HTTP请求的工作代码:如何将HTTP代理与Rust中的异步Hyper 0.11一起使用?,http,proxy,rust,hyper,Http,Proxy,Rust,Hyper,如何使用带有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(); le
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)
}
}
这不是的副本,因为我询问的是Hyper 0.11的新版本,该版本具有与以前版本不兼容的完全不同的API。您可以使用
客户端::配置()
方法构建代理连接器,然后使用句柄构建它,请参见下面的代码片段
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)
let handle=core.handle();
设代理={
让proxy_uri=“http://:port.parse().unwrap();
让mut proxy=proxy::new(Intercept::All,proxy\u uri);
代理。设置授权(基本{
用户名:“您的用户名”,
密码:一些(“您的密码”),
});
让http_connector=HttpConnector::new(4,&handle);
ProxyConnector::from_proxy(http_connector,proxy).unwrap()
};
让client=client::configure().connector(proxy).build(&handle)
现在,您可以使用客户端对象执行任何REST调用。您可能会从作者在名为reqwest Sergey的hyper 0.11之上构建的独立高级界面中找到很多灵感,上面的示例可以帮助您吗?我已经详细介绍了该主题,还没有尝试过。