Developers

Integrations

Guide

C# (.NET)

Send requests from C# through a Crawlhub proxy with HttpClient and WebProxy.

Your Crawlhub connection

FieldValue
GatewayUse the assigned host and port for your client’s protocol.
Residential usernameCopy the username from your configured residential connection.
ISP / Datacenter usernameUse the username and port assigned to your product.

Scroll horizontally to read all columns.

Use the protocol supported by this client. The examples below use PROXY_SERVER or separate host/port fields for these connection values. Read the proxy parameter reference.

Prepare the client

You need .NET 6 or newer. The example uses top-level statements, so it fits in one Program.cs file.

Create a project with dotnet new console, paste the example into Program.cs and start it with dotnet run. No extra package is needed.

Put your proxy login in environment variables, not in the code. Copy the username and password from your dashboard.

Set your proxy login
export PROXY_USERNAME="your-proxy-username"
export PROXY_PASSWORD="your-proxy-password"

Connection example

The example connects to http://proxy.crawlhub.ai:8000 and prints the exit IP that the target sees.

C# (.NET) proxy example
using System;
using System.Net;
using System.Net.Http;

var user = Environment.GetEnvironmentVariable("PROXY_USERNAME");
var pass = Environment.GetEnvironmentVariable("PROXY_PASSWORD");

var proxy = new WebProxy("http://proxy.crawlhub.ai:8000")
{
    Credentials = new NetworkCredential(user, pass)
};
var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true };
using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };

var ip = await client.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"Exit IP: {ip}");

Add a country or session

Add options to the end of the username. The password stays the same. __cc_us picks a US exit. __cc_us__sid_job01 picks a US exit and keeps the same IP while you reuse that session ID.

Username options
// US exit
var user = Environment.GetEnvironmentVariable("PROXY_USERNAME") + "__cc_us";

// Or a US exit that keeps the same IP for session job01
// var user = Environment.GetEnvironmentVariable("PROXY_USERNAME") + "__cc_us__sid_job01";

Put the country before the session. Read the proxy parameter reference for all options.

Reuse the client

Create one HttpClient for each proxy setting and keep it for the life of the app. A new HttpClient for every request opens new connections each time and can run out of sockets.

The proxy is fixed when the handler is built. If you need a second country or session, build a second handler and HttpClient for it.

Verify the connection

Run the example. It prints the exit IP, which is the address of the proxy, not your own. Run it again without a session ID and the IP can change.

Use a public HTTPS target on port 443. The gateway also forwards plain http:// on port 80. It refuses other ports and private or local addresses. Keep TLS verification on.

When it fails

407 means the proxy did not accept the login. Check the username, the password and the options you added. HttpClient throws an HttpRequestException that names status 407 (Proxy Authentication Required). A 407 can also mean that the trial ended or no traffic is left, so check your dashboard.

403 means the gateway refused the target, for example a port other than 80 or 443. 502 means the exit or the target did not answer. Retry after a short wait.

See Troubleshooting for other errors. Send your client version and the error text, without your password, to support.

Search documentation

Search guides, products and reference pages.