How to use a proxy server in C#?

11.04.2025

proxy server in c#

 

Proxy servers are a kind of “intermediary” between the user and the Internet. Naturally, this tool is used by developers in all programming languages. A proxy server in C# has wide functionality – it allows you to hide your real IP address, access any content, distribute the load between servers, etc.

We present to your attention a short guide to working with proxies in this programming language. We will teach you the basics – from basic setup to solving common problems that arise in real work.

What is a proxy server and why is it needed?

It is a computer or program that acts as an intermediary between the user and the Internet. All requests are sent through a proxy. The main reasons for their use:

  • Anonymity – they hide the user’s real IP.
  • Access to blocked resources – you can bypass regional restrictions.
  • Security – protects against some network threats.
  • Traffic optimization – caches data to speed up page loading.
  • Load distribution between servers.

There are public and private proxies. The first type is available to everyone, the second requires authorization. One of the best is mobile proxies from LTE Socks. They combine anonymity, performance, and stability. In fact, you get real IP addresses of mobile operators at your disposal, which means that for most sites you can’t be distinguished from ordinary mobile Internet users.

How to set up a proxy in C#?

There are two ways to set up a proxy in C#: using the outdated WebProxy or the more modern HttpClient. To get started, you need to prepare the working environment.

Preparing the working environment

Make sure you have all the necessary tools and libraries. For basic work with a proxy server in C# you will need:

  • .NET SDK 6.0+;
  • development environment (Visual Studio, VS Code);
  • System.Net and System.Net.Http namespaces.

You also need the HtmlAgilityPack library for parsing.

Setting up a proxy via WebProxy

How to connect a proxy in C# via WebProxy? Use the code, but keep in mind that this is an outdated method – a more modern one involves using HttpClient:

var proxy = new WebProxy(“http://proxy:port”);

proxy.Credentials = new NetworkCredential(“username”, “password”);

var request = WebRequest.Create(“http://example.com”);

request.Proxy = proxy;

Specify the proxy address, authorization data and apply the proxy to the request.

Proxy integration with HttpClient

how to set up proxy in c#

The modern way through HttpClientHandler:

var handler = new HttpClientHandler

{

Proxy = new WebProxy(“http://proxy:port”)

{

Credentials = new NetworkCredential(“username”, “password”)

},

UseProxy = true

};

using var client = new HttpClient(handler);

Here we configure a request handler with proxy parameters and pass it to HttpClient.

Using proxy in C# for different tasks

Proxy in C# can be used in different scenarios, from simple requests to complex web scraping and API integration. Let’s take a look at some of the most common tasks and approaches to solving them.

Using proxy for web requests

Example of sending JSON data via POST:

// Configuring HttpClient with proxy

var data = new {

username = “test”,

email = “test@example.com”

};

var json = JsonSerializer.Serialize(data);

var content = new StringContent(json, Encoding.UTF8, “application/json”);

var response = await client.PostAsync(url, content);

This code serializes the object to JSON, creates a StringContent object, sends POST via HttpClient, and receives the response.

Working with Web Scraping Proxy

Web scraping is the process of automatically retrieving data from websites. In practical work, you often need to send many requests, which may result in your IP address being blocked. Proxies help to solve this problem.

Proxy rotation is important for scraping:

// Function for scraping with proxy rotation

async Task ScrapeWithProxy(string url, Proxy[] proxies) {

// Selecting a random proxy

var proxy = GetRandomProxy(proxies); 

// Customizing the client

var client = CreateHttpClient(proxy);

// Sending a request

var html = await client.GetStringAsync(url);

// If an error occurs, run the function recursively with a new proxy

}

What we do in this code:

  • select a random proxy;
  • create an HttpClient;
  • handle errors and make a recursive call with the new proxy.

Here is the full scraper code:

using System;

using System.Collections.Generic;

using System.Net;

using System.Net.Http;

using System.Threading.Tasks;

using HtmlAgilityPack;

public class ProxyRotator

{

private readonly List<string> _proxies;

private readonly Random _random = new Random();

public ProxyRotator(List<string> proxies)

{

_proxies = proxies;

}

public string GetRandomProxy()

{

int index = _random.Next(_proxies.Count);

return _proxies[index];

}

}

async Task<string> ScrapeWithProxyRotationAsync(string url, ProxyRotator rotator)

{

// Getting a random proxy

string proxyUrl = rotator.GetRandomProxy();

// Configuring HttpClient with proxy

var handler = new HttpClientHandler

{

Proxy = new WebProxy(proxyUrl),

UseProxy = true,

// Add timeout for proxy connection

Proxy = { UseDefaultCredentials = false }

};

using var client = new HttpClient(handler)

{

Timeout = TimeSpan.FromSeconds(30) // Set the total timeout

};

try

{

// Simulate a normal browser

client.DefaultRequestHeaders.Add(“User-Agent”, “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36”);

client.DefaultRequestHeaders.Add(“Accept-Language”, “ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7”);

// Sending a request

var response = await client.GetAsync(url);

// If you get an error (e.g. 403 Forbidden), try with another proxy

if (!response.IsSuccessStatusCode)

{

Console.WriteLine($”Proxy {proxyUrl} returned the status {response.StatusCode}. Let’s try another one…”);

return await ScrapeWithProxyRotationAsync(url, rotator); // Recursive call with new proxy

string htmlContent = await response.Content.ReadAsStringAsync();

// Using HtmlAgilityPack for HTML parsing

var htmlDocument = new HtmlDocument();

htmlDocument.LoadHtml(htmlContent);

// Example of extracting all h1 headings

var h1Nodes = htmlDocument.DocumentNode.SelectNodes(“//h1”);

if (h1Nodes != null)

{

foreach (var node in h1Nodes)

{

Console.WriteLine($”Заголовок: {node.InnerText.Trim()}”);

}

}

return htmlContent;

}

catch (HttpRequestException ex)

{

Console.WriteLine($“Error when using a proxy {proxyUrl}: {ex.Message}“);

return await ScrapeWithProxyRotationAsync(url, rotator); // Trying with a different proxy

}

catch (TaskCanceledException)

{

Console.WriteLine($“Proxy timeout {proxyUrl}. Let’s try another one…”);

return await ScrapeWithProxyRotationAsync(url, rotator);

}

}

Connecting proxies for APIs and services

 

When working with external APIs and web services, a proxy server in C# is useful for bypassing request limits, providing fault tolerance and geographic distribution. It is convenient to create a common client to work with APIs:

public class ApiClient {

private HttpClient _client;

public ApiClient(string proxy) {

// Initializing a client with a proxy

}

public async Task<string> GetData(string url) {

return await _client.GetStringAsync(url);

}

}

// Utilization

var client = new ApiClient(proxy);

var data = await client.GetData(endpoint);

This way we can easily reuse proxy settings in different API requests.

When working with APIs, especially if we need to process a large number of requests, we should consider using mobile proxies from LTE Socks. They provide a stable connection and the ability to rotate IP addresses. This is important when integrating with APIs that have limitations on the number of requests.

Advantages and disadvantages of working with a proxy server in C#

proxy usage in c#

Working with a proxy server in C# has its pros and cons. The former include high anonymity and security in the network, access to blocked resources and the ability to distribute the load between servers to increase stability. If you wish, you can organize parallel requests from different IPs – this approach is useful in web scraping programs.

On the other hand, using proxies in C# complicates the code and requires additional settings. As a result, the time of code development and further support increases dramatically.

Frequent errors and their solutions when using proxies in C#

When working with proxies in C#, you may encounter such errors:

  • Authentication problems – check the login-password pair.
  • HTTPS – disable certificate validation.
  • Timeouts – add request timeouts.

In case of IP blocking, apply proxy rotation in your code.

Conclusion: how to effectively use a proxy server in C#?

First, you should choose a reliable vendor to work with proxies in C# – such as LTE Socks. Secondly, even good tools need to be changed from time to time – organize rotation in your code.

Anything can happen in the work of programs – write down how the scraper or your other application will handle errors. Specialists recommend using asynchronous calls – it increases the stability of your code.

Before using proxies in practice, you should test them in a checker. We recommend Proxy checker online. And also our company offers reliable sim card hosting – if you want, you can earn money with us. Proper proxy configuration will make your C# applications more stable and secure.

 

Read next

All article