Rust

This article provides an example of using a proxy to make an HTTP request with the Rust programming language.

Rust Code

In this example, we use the Residential proxy below:

residential.pingproxies.com:7777:customer-tt_pp_lz_5051-sessid-Z9tCGVYHu:23n22mk22

This proxy is split into four parts separated by colons. We first save the proxy as a string variable, then split it into four parts at each colon (:) and then add it, with authentication, to our proxy dictionary.

use reqwest;

#[tokio::main]
async fn main() {
    let proxy_string = "residential.pingproxies.com:7777:customer-tt_pp_lz_5051-sessid-Z9tCGVYHu:23n22mk22".to_string();
    let split_proxy: Vec<&str> = proxy_string.split(":").collect();

    match &split_proxy[..] {
        [ip, port, user, pass] => {
            let uri = format!("{}:{}", ip, port);
            let http = reqwest::Proxy::http(&uri).unwrap().basic_auth(user, pass);
            let https = reqwest::Proxy::https(&uri).unwrap().basic_auth(user, pass);

            let client = reqwest::Client::builder()
                .proxy(http)
                .proxy(https)
                .build()
                .unwrap();

            let res = client.get("http://ip-api.com/json").send().await.unwrap();
        }

        _ => panic!(),
    };
}

This requires tokio as an async driver and reqwest as a http client.

reqwest = "0.11.12"
tokio = { version = "1.12.2", features = ["full"] }

Last updated