Is there a way to use an HTTPS proxy in .NET 6?

Dominik Bakos 0 Reputation points
2023-02-16T13:00:00.3533333+00:00

I need to check if certain HTTPS proxies are working. If I try to access the base-address without the proxy, it works fine. Using the proxy I get an error saying I can only use HTTP or SOCKS proxies. The problem is, I wasn't able to find an alternate solution that supports HTTPS proxies. Here is my code so far:

var proxy = new WebProxy($"https://proxy-server:port");
    var handler = new HttpClientHandler { Proxy = proxy };

    using (var client = new HttpClient(handler))
    {
        client.BaseAddress = new Uri("https://base-address");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

        try
        {
            var content = client.GetStringAsync(filepath).Result;
            Console.WriteLine(content);
        }
        catch (Exception e)
        {
            Console.WriteLine("Failed. Reason: " + e);
        }
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,320 questions
{count} votes