Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClientHandler does not contain a definition for DefaultProxyCredentials

I am using the following code to get data from an API using .Net Core 2.0.

        using (var handler = new HttpClientHandler() { DefaultProxyCredentials = CredentialCache.DefaultCredentials })
        {
            handler.PreAuthenticate = true;
            using (var client = new HttpClient(handler))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                var response = await client.GetAsync(url);
            }
        }

However, I need to convert the project to .Net framework 4.6.1 and there is no DefaultProxyCredentials property in .Net Framework 4.6.1.

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.defaultproxycredentials

What is the equivalent of DefaultProxyCredentials in .Net framework 4.6.1 (without using a config file)?

Update: I tried changing the code to the following:

using (var handler = new HttpClientHandler() { Proxy = WebRequest.DefaultWebProxy})
{
}

It works but randomly throws 407 - "Proxy Authentication Required" error.

like image 401
developer Avatar asked Sep 03 '19 12:09

developer


1 Answers

Why are you targeting 4.6 instead of 4.7.2? This matters and can lead to NuGet dependency hell.


It looks like you're using the (very) old HttpClient class included in 4.6 instead of the System.Net.Http package. That old implementation doesn't even use the new socket handler. The HttpClientHandler.DefaultProxyCredentials property was added in .NET 4.7.1.

  • The best option is probably to target the latest .NET version, or at least 4.7.1 and use the same code you do now. Better yet, target 4.7.2 to avoid the dependency shim hell explained in the next option.
  • You could just add the package. It's the same .NET Standard 2.0 package used in .NET Core projects. The downside to that is that 4.6.1 is not really .NET Standard 2.0 compatible and requires several compatibility libraries. Upgrading can easily lead to dependency hell as versions conflict with one another. The .NET team admitted that trying to retrofit .NET Standard 2.0 compliance was a bad idea

While NuGet considers .NET Framework 4.6.1 as supporting .NET Standard 1.5 through 2.0, there are several issues with consuming .NET Standard libraries that were built for those versions from .NET Framework 4.6.1 projects. For .NET Framework projects that need to use such libraries, we recommend that you upgrade the project to target .NET Framework 4.7.2 or higher.

Been there. Have the production crash reports to prove it. And the premium 1 day upgrade experience of removing all previous shims to get rid of version conflicts. Several issues indeed

  • Set the Credentials property of the default proxy with
    WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
like image 128
Panagiotis Kanavos Avatar answered Oct 19 '22 23:10

Panagiotis Kanavos