# Request Example Code

### Using HTTP/Socks5 Proxy in C++ (with Username and Password)

#### HTTP Proxy + Username and Password (Complete Example)

```
#include <curl/curl.h>
#include <iostream>
static size_t write_cb(void* ptr, size_t size, size_t nmemb, void* userdata) {
    std::string* resp = (std::string*)userdata;
    resp->append((char*)ptr, size * nmemb);
    return size * nmemb;
}
int main() {
    CURL* curl = curl_easy_init();
    if (!curl) return -1;
    std::string response;
    curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/ip");
    // HTTP proxy
    curl_easy_setopt(curl, CURLOPT_PROXY, "http://127.0.0.1:8080");
    // Proxy username and password
    curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, "user");
    curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, "pass");
    // Automatically detect HTTP / HTTPS
    curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        std::cerr << curl_easy_strerror(res) << std::endl;
    } else {
        std::cout << response << std::endl;
    }
    curl_easy_cleanup(curl);
    return 0;
}
```

#### SOCKS5 Proxy + Username and Password (Complete Example)

```
#include <curl/curl.h>
#include <iostream>
static size_t write_cb(void* ptr, size_t size, size_t nmemb, void* userdata) {
    std::string* resp = (std::string*)userdata;
    resp->append((char*)ptr, size * nmemb);
    return size * nmemb;
}
int main() {
    CURL* curl = curl_easy_init();
    if (!curl) return -1;
    std::string response;
    curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/ip");
    // SOCKS5 proxy
    curl_easy_setopt(curl, CURLOPT_PROXY, "socks5h://127.0.0.1:1080");
    // socks5h = remote DNS (very important)
    // SOCKS5 authentication
    curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, "socksUser");
    curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, "socksPass");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        std::cerr << curl_easy_strerror(res) << std::endl;
    } else {
        std::cout << response << std::endl;
    }
    curl_easy_cleanup(curl);
    return 0;
}

```

### Using HTTP/Socks5 Proxy in Go (with Username and Password)

#### Method 1: net/http (Most Common)

```
package main
import (
    "fmt"
    "io"
    "net/http"
    "net/url"
    "time"
)
func main() {
    proxyURL, _ := url.Parse("http://user:pass@127.0.0.1:8080")
    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyURL),
    }
    client := &http.Client{
        Transport: transport,
        Timeout:   10 * time.Second,
    }
    resp, err := client.Get("https://httpbin.org/ip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    body, _ := io.ReadAll(resp.Body)
    fmt.Println(string(body))
}

```

#### Method 2: golang.org/x/net/proxy

```
package main
import (
    "context"
    "fmt"
    "io"
    "net"
    "net/http"
    "time"
    "golang.org/x/net/proxy"
)
func main() {
    // SOCKS5 authentication
    auth := &proxy.Auth{
        User:     "socksUser",
        Password: "socksPass",
    }
    // Create SOCKS5 Dialer
    socksDialer, err := proxy.SOCKS5(
        "tcp",
        "127.0.0.1:1080",
        auth,
        proxy.Direct,
    )
    if err != nil {
        panic(err)
    }
    // Inject into HTTP Transport
    transport := &http.Transport{
        DialContext: func(
            ctx context.Context,
            network, addr string,
        ) (net.Conn, error) {
            return socksDialer.Dial(network, addr)
        },
    }
    client := &http.Client{
        Transport: transport,
        Timeout:   10 * time.Second,
    }
    resp, err := client.Get("https://httpbin.org/ip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    body, _ := io.ReadAll(resp.Body)
    fmt.Println(string(body))
}

```

### Using HTTP/Socks5 Proxy in Java (with Username and Password)

#### HTTP Proxy + Username and Password

```
import org.apache.http.*;
import org.apache.http.auth.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*;
import org.apache.http.protocol.HttpContext;
import java.io.*;
import java.net.*;
public class ApacheHttpProxy {
    public static void main(String[] args) throws Exception {
        CredentialsProvider creds = new BasicCredentialsProvider();
        creds.setCredentials(
                new AuthScope("127.0.0.1", 8080),
                new UsernamePasswordCredentials("httpUser", "httpPass")
        );
        HttpHost proxy = new HttpHost("127.0.0.1", 8080);
        CloseableHttpClient client = HttpClients.custom()
                .setDefaultCredentialsProvider(creds)
                .setProxy(proxy)
                .build();
        HttpGet get = new HttpGet("https://httpbin.org/ip");
        try (CloseableHttpResponse resp = client.execute(get)) {
            System.out.println(EntityUtils.toString(resp.getEntity()));
        }
    }
}

```

#### SOCKS5 + Username and Password

```
import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*;
import org.apache.http.protocol.HttpContext;
import org.apache.http.config.*;
import java.io.IOException;
import java.net.*;
public class ApacheSocks5Proxy {
    public static void main(String[] args) throws Exception {
        // SOCKS5 authentication
        ProxyAuth.install("socksUser", "socksPass");
        ConnectionSocketFactory socksFactory = new PlainConnectionSocketFactory() {
            @Override
            public Socket createSocket(HttpContext context) throws IOException {
                return new Socket(new Proxy(
                        Proxy.Type.SOCKS,
                        new InetSocketAddress("127.0.0.1", 1080)
                ));
            }
        };
        Registry<ConnectionSocketFactory> registry =
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", socksFactory)
                        .register("https", SSLConnectionSocketFactory.getSocketFactory())
                        .build();
        PoolingHttpClientConnectionManager cm =
                new PoolingHttpClientConnectionManager(registry);
        CloseableHttpClient client =
                HttpClients.custom().setConnectionManager(cm).build();
        HttpGet get = new HttpGet("https://httpbin.org/ip");
        try (CloseableHttpResponse resp = client.execute(get)) {
            System.out.println(EntityUtils.toString(resp.getEntity()));
        }
    }
}

```

### Using HTTP/Socks5 Proxy in Python (with Username and Password)

#### HTTP Proxy + Username and Password

```
import requests
proxies = {
    "http":  "http://user:pass@127.0.0.1:8080",
    "https": "http://user:pass@127.0.0.1:8080",
}
resp = requests.get(
    "https://httpbin.org/ip",
    proxies=proxies,
    timeout=10
)
print(resp.text)

```

#### SOCKS5 Proxy + Username and Password

```
import requests
proxies = {
    "http":  "socks5://user:pass@127.0.0.1:1080",
    "https": "socks5://user:pass@127.0.0.1:1080",
}
resp = requests.get(
    "https://httpbin.org/ip",
    proxies=proxies,
    timeout=10
)
print(resp.text)

```

### Using HTTP/Socks5 Proxy in Node.js (with Username and Password)

#### HTTP Proxy + Username and Password (Recommended Libraries)

```
npm install axios http-proxy-agent https-proxy-agent
```

```
import axios from "axios";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";

const proxyUrl = "http://user:pass@127.0.0.1:8080";

const httpAgent = new HttpProxyAgent(proxyUrl);
const httpsAgent = new HttpsProxyAgent(proxyUrl);

const client = axios.create({
  httpAgent,
  httpsAgent,
  timeout: 10000,
});

(async () => {
  const res = await client.get("https://httpbin.org/ip");
  console.log(res.data);
})();

```

#### SOCKS5 Proxy + Username and Password (Recommended Libraries)

```
npm install axios socks-proxy-agent
```

```
import axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";

const proxyUrl = "socks5://user:pass@127.0.0.1:1080";
// socks5h:// can be used for remote DNS (supported in newer versions)

const agent = new SocksProxyAgent(proxyUrl);

const client = axios.create({
  httpAgent: agent,
  httpsAgent: agent,
  timeout: 10000,
});

(async () => {
  const res = await client.get("https://httpbin.org/ip");
  console.log(res.data);
})();

```
