0

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 !

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I'll give a shot to that thanks hope I'll be able to get my script functioning
I would need my script to change a few times the proxy address while it's running. If I'm doing what you told me to do, if I only set a new proxy address and new port in the middle of the script will that change the proxy or it will still be the same ? (sorry I'm new to C#) Thanks for helping tho !
If you know at compile time the proxy addresses you could create a dictionary of them (i will update the answer with an example of it) otherwise keep track of this issue: github.com/dotnet/runtime/issues/35992

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.