# Use a proxy with Rust reqwest

Complete proxy requests using reqwest Proxy, account authentication, asynchronous client, and timeout.

Rust asynchronous services can use the caller's own locked projects `reqwest` And Tokyo. Agent Certification Pass `Proxy` Configuration, not written into the target request header.

## Precondition

- Caller project installs and locks itself `reqwest` With Tokio.
- The account is closed. `us.zzhttp.com:7878`; Injection through controlled environment `PROXY_USERNAME` and `PROXY_PASSWORD`.
- Set a concurrent limit and a total request timeout for the service.

## Complete example

```rust
use reqwest::{Client, Proxy};
use std::{env, error::Error, time::Duration};

/// Asynchronously check the public exit IP through an authenticated proxy.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let proxy_username = env::var("PROXY_USERNAME")?;
    let proxy_password = env::var("PROXY_PASSWORD")?;
    let proxy = Proxy::all("http://us.zzhttp.com:7878")?
        .basic_auth(&proxy_username, &proxy_password);
    let client = Client::builder()
        .proxy(proxy)
        .connect_timeout(Duration::from_secs(10))
        .timeout(Duration::from_secs(30))
        .build()?;

    let body = client
        .get("https://ipinfo.io/json")
        .send()
        .await?
        .error_for_status()?
        .text()
        .await?;
    println!("{body}");
    Ok(())
}
```

## Expected results

- `Proxy::all` Unify HTTP and HTTPS targets to the current HTTP proxy.
- Clients reuse connections and limit the connection phase and the total request time, respectively.
- Returns the proxy export detection body as a success.

## Error handling

- 407 Inspection `.basic_auth` The agent accounts and current interests.
- Connection and request timeouts are used for the network establishment phase and the target response phase, respectively.
- Performs limited retries with shaking only on equality requests.

## Safety tips

Do not use `Debug` Output `Proxy` Or client configuration. Error reports carry only the error category, task identifier and desensitized target host, and do not record the content of the proxy authentication.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
