Making HTTP requests to remote servers is a fundamental task when using any programming language, whether you’re fetching data from APIs or performing web scraping tasks. In the process, however, many developers encounter issues such as blocked requests due to high frequency. Luckily, there's a simple fix to the issue: using a proxy. In this tutorial, we’ll take a look at how to use proxies in the C# language using the HttpClient class.
First of all, let’s create a .NET project, where we can test out our C# code. The most popular way to start writing .NET applications would be to create a project in Visual Studio and let the IDE handle the rest. However, for this example, we’ll be using the dotnet CLI to create a simple console application with C#.
To begin with, let’s install the CLI tool first.
If you’re a Windows user, you can simply download .NET here, run the installer and the CLI tool should be automatically available to you in Powershell, or CMD.
For macOS and Linux, you can use your local package manager to do that. Some examples:
macOS:
brew install dotnet
Linux (Debian based):
sudo apt-get install -y dotnet-sdk-9.0
Once that’s done, you can simply run these commands in your terminal:
mkdir proxy-example
cd proxy-example
dotnet new console
You should now have a new folder called proxy-example with some files inside. These files are what make the folder a console application project in the .NET framework. You can find a file called Program.cs, which is the entry point of the project.
Let’s open that file, as we’ll be using it to send HTTP requests through proxies with C# later on in the tutorial.
A proxy is an intermediary between your application and another server or resource you’re trying to access. Using one allows you to appear as though your requests are coming from the proxy instead of your device.
This is particularly useful in scenarios where direct HTTP requests might be blocked or monitored. For example, most websites try to restrict web scraping tasks in one way or another. However, by routing your traffic through a proxy, you can send your requests that go through servers with real IP addresses in locations of your choosing, provided by local ISPs. As a result, your requests look like regular HTTP requests sent by regular users, instead of an application performing web scraping tasks.
Browse our extensive proxy catalogue and choose the best suited option for your scraping project.
Let’s go back to the Program.cs file we created before.
The initial created file should contain a simple “Hello world!” write line command. Let’s expand the file a little bit, so it can be utilized as a class, rather than a one line application.
Add these lines to your C# file:
using System;
namespace Example.ProxyApp;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
After this, we can make the main function be an asynchronous Task, which is usually a standard way to make HttpClient proxy requests in C# with the HttpClient class, due to inherently better performance.
To do that, simply change the return type of the method to Task from void, and add an async keyword before the return type definition.
You should also add the System.Threading.Tasks import, as it contains the definitions for types like Task, and provides general support for asynchronous programming.
The code should look something like this:
using System;
using System.Threading.Tasks;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
After that’s done, we can begin forming our HttpClient proxy request.
First off, import the System.Net.Http directive, to gain access to the native C# HTTP client. After that, we can delete the Console.WriteLine(“Hello World!”) line and define our HttpClient instance.
using System;
using System.Threading.Tasks;
using System.Net.Http;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
}
}
}
Notice, we’re defining the HttpClient instance inside the using statement. This is necessary for disposing the connections used by the client once it’s done making requests.
We can now use the defined new HttpClient instance to perform an asynchronous HTTP request. Let’s use the Oxylabs IP location endpoint to test it out:
using System;
using System.Threading.Tasks;
using System.Net.Http;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://ip.oxylabs.io/location");
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
If you run the code, you should see an object in string format showing your current IP address with more details about its origin and location.
Now that we know how to send a basic HTTP GET request in C#, let’s call the same Oxylabs IP location endpoint with a proxy server and see how your location is presented then.
In this example, we’ll use Oxylabs Residential Proxies and the backconnect entry hostname. To start with, let’s import the System.Net directive to gain access to the required proxy objects, and define the proxy variable in our main method. The code should look something like this:
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
var proxy = new WebProxy("https://pr.oxylabs.io:7777");
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://ip.oxylabs.io/location");
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
The proxy is defined with the WebProxy class that accepts a hostname as a parameter.
We then need to define a proxy var HttpClientHandler object, which allows us to assign the proxy to our HTTP client. We can declare the use of the previously defined proxy in the client handler.
It should look like this:
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
var proxy = new WebProxy("https://pr.oxylabs.io:7777");
var httpClientHandler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true
};
using (HttpClient client = new HttpClient(httpClientHandler))
{
HttpResponseMessage response = await client.GetAsync("https://ip.oxylabs.io/location");
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
That’s it – we’ve successfully defined a proxy server that we can use to make the HTTP GET request to the Oxylabs IP location endpoint.
If we used a server that doesn’t have proxy authentication required, the code above would suffice. However, most proxy providers do require authentication to restrict unauthorized access. In other words, you’ll need an authenticated proxy.
To achieve this with the Oxylabs Residential Proxy, we simply need to pass the proxy credentials found in the dashboard, under the Residential Proxies > Users section.
After that, we can create a new Credentials object as a parameter in the WebProxy class.
The object should be an instance of the NetworkCredential class, that includes our proxy credentials passed as parameters.
The following output should look something like this:
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace Example.ProxyApp;
internal class Program
{
static async Task Main(string[] args)
{
var proxy = new WebProxy("https://pr.oxylabs.io:7777");
{
Credentials = new NetworkCredential("USERNAME", "PASSWORD")
};
var httpClientHandler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true
};
using (HttpClient client = new HttpClient(httpClientHandler))
{
HttpResponseMessage response = await client.GetAsync("https://ip.oxylabs.io/location");
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
Make sure to replace the credential placeholders with your own proxy credentials. If you run the code above, you should see an IP address different from the one you saw before, with a location different from your own as well.
This indicates that an authenticated proxy is successfully used in your HTTP request.
Before wrapping up, let’s look at a few general tips for making the most of using proxies. These tips are for when you use random proxy IPs (usually, free and shared):
Use HTTPS instead of HTTP when connecting to your proxy server. HTTPS provides a secure, encrypted connection between your device and the proxy. We have dedicated blog posts explaining HTTP vs. HTTPS and HTTP headers in detail. Also see: HTTPX vs Requests vs AIOHTTP.
Rotate between multiple proxies to prevent downtime. Using a single random proxy in a production environment can cause your application to lose any contact with the outside world, if the proxy goes down for any reason. It’s best to maintain a collection of proxies and rotate the proxy IPs periodically.
Handle errors when connecting to your proxy. As mentioned above, losing connection to your proxy can lead to long downtime for your application.
Therefore, you should handle any type of error that can occur when connecting to a proxy. Try implementing a retry mechanism and provide meaningful error messages in your applications logs, so that it’s easier to debug a potential issue.
On the other hand, if you use a premium provider like Oxylabs, integrating proxies in your application is a simple task. You don’t have to worry about choosing protocols, rotating proxies or optimizing performance within your application.
Using proxies with HttpClient in C# is essential for handling blocked requests, improving anonymity, and ensuring stable connections when making HTTP calls. This guide walks you through setting up a proxy in C#, from basic proxy configuration to authentication. If you're looking to go deeper into web scraping with C#, be sure to check out our dedicated tutorial.
Finally, if you want to see HttpClient and proxies in action, you can give our free proxies a try – we offer 5 Datacenter IPs, no credit card required. Or, if you’re working on a large-scale project, check out our rotating Residential Proxies instead: we have the fastest and largest IP pool of 100M+ ethically-sourced IPs.
About the author
Roberta Aukstikalnyte
Senior Content Manager
Roberta Aukstikalnyte is a Senior Content Manager at Oxylabs. Having worked various jobs in the tech industry, she especially enjoys finding ways to express complex ideas in simple ways through content. In her free time, Roberta unwinds by reading Ottessa Moshfegh's novels, going to boxing classes, and playing around with makeup.
All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.
Get the latest news from data gathering world
Scale up your business with Oxylabs®