First of all i would highly recommend you to use HttpClient instead of WebClient, Microsoft suggests it as well (https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated), because its obsolete.
Using HttpClient you could go as bellow:
First create an instance of WebProxy with your proxy's address/port
var address = "your address";
int port = //your port
var webProxy = new WebProxy(address, port)
{
BypassProxyOnLocal = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("your-username", "your-pass")
};
Then create a SocketsHttpHandler like
SocketsHttpHandler handler = new()
{
Proxy = webProxy,
UseProxy = true
};
And finally pass this handler as ctor parameter to HttpClient like
HttpClient client = new HttpClient(handler);
// An example of http client's usage would be this one
using var response = await client.GetAsync("request-uri");
var responseText = await response.Content.ReadAsStringAsync();
UPDATE Using DI
This is a general example of DI usage
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
var proxies = ProxyProfile.GetProxyList(firstPort, lastPort, address, username, password);
foreach (var proxy in proxies)
{
services.AddHttpClient(proxy.Key)
.ConfigurePrimaryHttpMessageHandler(() =>
GetPrimaryHandler(proxy.Value));
}
}).Build();
public static SocketsHttpHandler GetPrimaryHandler(IWebProxy proxy, bool useCookies = true)
{
return new SocketsHttpHandler
{
Proxy = proxy,
UseProxy = true,
UseCookies = useCookies,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
AllowAutoRedirect = true,
ConnectTimeout = TimeSpan.FromSeconds(15),
PooledConnectionIdleTimeout = TimeSpan.FromSeconds(15)
};
}
public static class ProxyProfile
{
public static IDictionary<string, IWebProxy> GetProxyList(int firstPort, int lastPort, string address, string username, string password)
{
Dictionary<string, IWebProxy> proxies = new();
for (int port = firstPort; port < lastPort; port++)
{
WebProxy webProxy = new(address, port)
{
BypassProxyOnLocal = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(username, password)
};
proxies.Add($"{port}", webProxy);
}
return proxies;
}
}
This way using dependency injection we "load" all the available proxy addresses and then we can change proxy dynamically in our code via named clients (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0#named-clients). In the consumer class we can inject a IHttpClientFactory
public Consumer(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
and then we can use some method like this:
private void CreateHttpClient()
{
var standardClient = _clientFactory.CreateClient("in this example we use the port as name");
}
Before start using it i recommend you to take a look at this docs https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests