Skip to content
Hopmind

Guides

How to send requests through a proxy in C# (.NET)

A request through an HTTP proxy with a login and password in C# using HttpClientHandler, WebProxy and NetworkCredential.

What you need

The proxy is issued in the format host:port:login:password. Replace HOST, PORT, LOGIN and PASSWORD with your own values. A modern .NET is required (the example uses top-level statements, that is, .NET 6 or newer). No extra packages are needed.

Example

Program.cs in a console project.
using System.Net;

// The login and password are passed with a NetworkCredential object, no need to encode them.
// The proxy scheme is http, including for HTTPS sites
var proxy = new WebProxy(
    new Uri("http://HOST:PORT"),
    false,
    null,
    new NetworkCredential("LOGIN", "PASSWORD"));

var handler = new HttpClientHandler
{
    Proxy = proxy,
    UseProxy = true,
};

// Without a timeout a request can wait too long
using var client = new HttpClient(handler)
{
    Timeout = TimeSpan.FromSeconds(30),
};

string ip = await client.GetStringAsync("https://api.ipify.org");

// The service returns the address the request came from
Console.WriteLine(ip);

If SOCKS5

We do not describe SOCKS5 setup for HttpClient in this guide: we could not confirm it in the Microsoft documentation. Use the proxy's HTTP port.

Common errors

  1. 407 Proxy Authentication Required: the login or password is wrong. Check the order in the issued host:port:login:password.
  2. TaskCanceledException or HttpRequestException on a timeout: check HOST and PORT, whether the SOCKS5 port was put into HTTP mode, and increase Timeout.
  3. Special characters in the password: NetworkCredential takes the password as is. If you write the login and password directly in a Uri address, you have to encode them with Uri.EscapeDataString.
  4. The https:// scheme in the proxy address: the issued proxy works over HTTP, so use http://HOST:PORT, even for HTTPS sites.
  5. For local addresses (flat names, loopback) the proxy may be bypassed automatically. Test against an external site, as in the example.
  6. Create one HttpClient and reuse it instead of creating a new one for every request.

Sources

Checked against the official documentation: October 2026. Program interfaces change: if item names differ, follow the documentation.

All guides are collected on the Guides page.

Find a proxy in the chat