essie98927
essie98927

Reputation: 1

How to get any web interaction go through a proxy in a C# code?

I would like to make any kind of web interaction in my code go through a proxy but I cannot find how to do that. Been searching on the Microsoft doc but can't figure out how to make it work...

Here's a sample of my code where I make the request:

int count = 0;
List<string> Links = new List<string>();
using (WebClient wc = new WebClient())
{

  string s = wc.DownloadString("https://www.google.com/search?q=site:drive.google.com+" + resp + "|");
  Regex r = new Regex(@"https:\/\/drive.google.com\/\w+\/\w+");
  foreach (Match m in r.Matches(s))
  {
    count++;
    Links.Add(m.ToString());
  }

Any help would be much appreciated thanks !

Upvotes: 0

Views: 349

Answers (1)

ggeorge
ggeorge

Reputation: 1630

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

Upvotes: 1

Related Questions