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.
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.
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
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With