Rust Reqwest Proxy Integration
The reqwest crate is the de facto HTTP client for Rust. It supports proxy configuration through the `Proxy` builder, including HTTP, HTTPS, and SOCKS5 protocols.
Cargo.toml Dependencies
[dependencies]
reqwest = { version = "0.12", features = ["json", "socks"] }
tokio = { version = "1", features = ["full"] }Basic HTTP Proxy
#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let proxy = Proxy::all("http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080")?;
let client = reqwest::Client::builder() .proxy(proxy) .timeout(std::time::Duration::from_secs(30)) .build()?;
let resp = client.get("https://httpbin.org/ip").send().await?; println!("Status: {}", resp.status()); println!("Body: {}", resp.text().await?);
Ok(()) } ```
SOCKS5 Proxy
Enable the `socks` feature in your Cargo.toml, then use the socks5 scheme:
let client = reqwest::Client::builder() .proxy(proxy) .build()?; ```
Sticky Sessions
let session_user = "YOUR_USERNAME-session-rust001";
let proxy_url = format!(
"http://{}:YOUR_PASSWORD@gate.hexproxies.com:8080",
session_user
);
let proxy = Proxy::all(&proxy_url)?;Concurrent Proxied Requests
let urls = vec![ "https://httpbin.org/ip", "https://httpbin.org/headers", "https://httpbin.org/get", ];
let tasks: Vec<_> = urls.into_iter().map(|url| { let client = client.clone(); tokio::spawn(async move { match client.get(url).send().await { Ok(resp) => println!("{}: {}", url, resp.status()), Err(e) => eprintln!("{}: {}", url, e), } }) }).collect();
join_all(tasks).await; ```
Retry with Backoff
async fn fetch_with_retry(
client: &reqwest::Client,
url: &str,
max_retries: u32,
) -> Result<String, reqwest::Error> {
let mut last_err = None;
for attempt in 0..max_retries {
match client.get(url).send().await {
Ok(resp) if resp.status().is_success() => {
return resp.text().await;
}
Ok(resp) if resp.status().as_u16() == 429 || resp.status().as_u16() == 503 => {
tokio::time::sleep(std::time::Duration::from_secs(2u64.pow(attempt))).await;
}
Ok(resp) => {
eprintln!("Non-retryable status: {}", resp.status());
return resp.text().await;
}
Err(e) => {
last_err = Some(e);
tokio::time::sleep(std::time::Duration::from_secs(2u64.pow(attempt))).await;
}
}
}
Err(last_err.unwrap())
}Environment-Based Configuration
let proxy_url = std::env::var("HEX_PROXY_URL")
.expect("HEX_PROXY_URL must be set");
let proxy = Proxy::all(&proxy_url)?;Reqwest also respects the standard `HTTP_PROXY` and `HTTPS_PROXY` environment variables if no explicit proxy is set on the client builder.