Mobile Proxy for C# / .NET
In .NET you route an HttpClient through a proxy by handing it an HttpClientHandler whose Proxy is a WebProxy with NetworkCredential. This guide wires that to a mobile proxy and rotates the IP against the switch API.
Prerequisites
- →.NET 6+ SDK (
dotnet --version). Everything here is in the BCL — no NuGet packages. - →A mobile proxy slot, port, username, password and API key from mobileproxies.org.
- →Credentials read from environment variables, never hard-coded.
Step-by-Step Configuration
Read credentials from the environment
using System;
string host = Environment.GetEnvironmentVariable("MP_HOST")!; // proxy.mobileproxies.org
int port = int.Parse(Environment.GetEnvironmentVariable("MP_HTTP_PORT")!); // 8000
string user = Environment.GetEnvironmentVariable("MP_USER")!;
string pass = Environment.GetEnvironmentVariable("MP_PASS")!;
string slot = Environment.GetEnvironmentVariable("MP_SLOT")!;
string apiKey = Environment.GetEnvironmentVariable("MP_API_KEY")!;HttpClientHandler with WebProxy
using System.Net;
using System.Net.Http;
var proxy = new WebProxy($"http://{host}:{port}")
{
Credentials = new NetworkCredential(user, pass),
// Send all traffic through the proxy, including localhost
BypassProxyOnLocal = false,
};
var handler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true,
};
var client = new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(30),
};Send a request
using System;
HttpResponseMessage response =
await client.GetAsync("https://example.com");
Console.WriteLine($"status: {(int)response.StatusCode}");Reuse one client (don't recreate per request)
HttpClient is meant to be long-lived. Create one per proxy configuration and share it — recreating it per request exhausts sockets under load.
public static class Mp
{
public static readonly HttpClient Client = BuildProxiedClient();
private static HttpClient BuildProxiedClient()
{
var proxy = new WebProxy(
$"http://{Environment.GetEnvironmentVariable("MP_HOST")}:" +
$"{Environment.GetEnvironmentVariable("MP_HTTP_PORT")}")
{
Credentials = new NetworkCredential(
Environment.GetEnvironmentVariable("MP_USER"),
Environment.GetEnvironmentVariable("MP_PASS")),
};
return new HttpClient(new HttpClientHandler { Proxy = proxy });
}
}Verify It Works
Fetch api.ipify.org through the client and print the egress IP. It should be a carrier-owned mobile IP, never your machine's real address.
string ip = await client.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"Egress IP: {ip}"); // e.g. 100.42.x.x on a mobile ASNRotate the IP
Request a fresh carrier IP with a POST to the switch endpoint. This call goes direct (a plain HttpClient, not the proxied one) and authenticates with your API key:
using System.Net.Http.Headers;
async Task RotateAsync(string slot, string apiKey)
{
using var direct = new HttpClient();
var request = new HttpRequestMessage(
HttpMethod.Post,
$"https://buy.mobileproxies.org/api/v1/proxies/{slot}/switch");
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", apiKey);
var resp = await direct.SendAsync(request);
Console.WriteLine($"rotate -> {(int)resp.StatusCode}");
await Task.Delay(TimeSpan.FromSeconds(4)); // let the new IP bind
}Troubleshooting
407 Proxy Authentication Required
The WebProxy.Credentials were not set or were empty. Build a NetworkCredential(user, pass) and confirm both values came through from the environment.
SocketException / port exhaustion under load
You are creating a new HttpClient per request. Reuse one instance (Step 04) or register it via IHttpClientFactory so sockets are pooled correctly.
IP doesn't change after a rotate
.NET keeps connections alive. After switching, allow a few seconds for the pooled connection to recycle, or set a short PooledConnectionLifetime on a SocketsHttpHandler.
Related Guides
Run .NET Through Mobile IPs
$5 trial. Wire up the WebProxy, point it at a carrier IP, and rotate on the API.