有条件地使用 ProxyConnector 或 HTTPConnector
Conditionally Use ProxyConnector or HTTPConnector
我想使用 hyper crate 发出 HTTP 请求。如果用户提供了代理设置,则请求必须通过代理,否则请求将不通过代理发送。
这是我的方法:-
use hyper::Client;
use hyper::client::HttpConnector;
use hyper_proxy::Intercept;
use hyper_proxy::Proxy;
use hyper_proxy::ProxyConnector;
fn main(){
let proxy_url_opt:Option<String> = Some(String::from("http://ip-address:port"))
let client = match proxy_url_opt { // Line 68
Some(proxy_url)=>{
let uri_str = &proxy_url;
let proxy_uri = uri_str.parse().unwrap();
let mut proxy = Proxy::new(Intercept::All, proxy_uri);
let connector = HttpConnector::new();
let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
let client = Client::builder().build(proxy_connector);
client // Line 80
}
None=>{
Client::new() // Line 83
}
};
// while condition {
let response = client.request(request).await?;
// }
}
但是这段代码给我错误。
match arms have incompatible types
expected struct `hyper_proxy::ProxyConnector`, found struct `hyper::client::connect::http::HttpConnector`
note: expected type `hyper::client::Client<hyper_proxy::ProxyConnector<hyper::client::connect::http::HttpConnector>, _>`
found struct `hyper::client::Client<hyper::client::connect::http::HttpConnector, hyper::body::body::Body>`rustc(E0308)
main.rs(68, 18): `match` arms have incompatible types
main.rs(80, 13): this is found to be of type `hyper::client::Client<hyper_proxy::ProxyConnector<hyper::client::connect::http::HttpConnector>, _>`
main.rs(83, 13): expected struct `hyper_proxy::ProxyConnector`, found struct `hyper::client::connect::http::HttpConnector`
rust 的解决方法是什么?
在 yorodm 的支持下,我使用枚举解决了它。我就是这样解决的:-
enum Client {
Proxy(HyperClient<ProxyConnector<HttpConnector>>),
Http(HyperClient<HttpConnector>)
}
impl Client {
pub fn request(&self, mut req: Request<Body>) -> ResponseFuture{
match self {
Client::Proxy(client)=>{
client.request(req)
}
Client::Http(client)=>{
client.request(req)
}
}
}
}
我想使用 hyper crate 发出 HTTP 请求。如果用户提供了代理设置,则请求必须通过代理,否则请求将不通过代理发送。
这是我的方法:-
use hyper::Client;
use hyper::client::HttpConnector;
use hyper_proxy::Intercept;
use hyper_proxy::Proxy;
use hyper_proxy::ProxyConnector;
fn main(){
let proxy_url_opt:Option<String> = Some(String::from("http://ip-address:port"))
let client = match proxy_url_opt { // Line 68
Some(proxy_url)=>{
let uri_str = &proxy_url;
let proxy_uri = uri_str.parse().unwrap();
let mut proxy = Proxy::new(Intercept::All, proxy_uri);
let connector = HttpConnector::new();
let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
let client = Client::builder().build(proxy_connector);
client // Line 80
}
None=>{
Client::new() // Line 83
}
};
// while condition {
let response = client.request(request).await?;
// }
}
但是这段代码给我错误。
match arms have incompatible types
expected struct `hyper_proxy::ProxyConnector`, found struct `hyper::client::connect::http::HttpConnector`
note: expected type `hyper::client::Client<hyper_proxy::ProxyConnector<hyper::client::connect::http::HttpConnector>, _>`
found struct `hyper::client::Client<hyper::client::connect::http::HttpConnector, hyper::body::body::Body>`rustc(E0308)
main.rs(68, 18): `match` arms have incompatible types
main.rs(80, 13): this is found to be of type `hyper::client::Client<hyper_proxy::ProxyConnector<hyper::client::connect::http::HttpConnector>, _>`
main.rs(83, 13): expected struct `hyper_proxy::ProxyConnector`, found struct `hyper::client::connect::http::HttpConnector`
rust 的解决方法是什么?
在 yorodm 的支持下,我使用枚举解决了它。我就是这样解决的:-
enum Client {
Proxy(HyperClient<ProxyConnector<HttpConnector>>),
Http(HyperClient<HttpConnector>)
}
impl Client {
pub fn request(&self, mut req: Request<Body>) -> ResponseFuture{
match self {
Client::Proxy(client)=>{
client.request(req)
}
Client::Http(client)=>{
client.request(req)
}
}
}
}