简体   繁体   English

HttpClientHandler不包含DefaultProxyCredentials的定义

[英]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. 我使用以下代码从使用.Net Core 2.0的API获取数据。

        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. 但是,我需要将项目转换为.Net framework 4.6.1,并且.Net Framework 4.6.1中没有DefaultProxyCredentials属性。

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.defaultproxycredentials 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)? 什么是.Net framework 4.6.1中的DefaultProxyCredentials(不使用配置文件)?

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. 它工作但随机抛出407 - “需要代理身份验证”错误。

Why are you targeting 4.6 instead of 4.7.2? 为什么你的目标是4.6而不是4.7.2? This matters and can lead to NuGet dependency hell. 这很重要,可能导致NuGet依赖地狱。


It looks like you're using the (very) old HttpClient class included in 4.6 instead of the System.Net.Http package. 看起来你正在使用4.6中包含的(非常)旧的HttpClient类而不是System.Net.Http包。 That old implementation doesn't even use the new socket handler. 旧的实现甚至不使用新的套接字处理程序。 The HttpClientHandler.DefaultProxyCredentials property was added in .NET 4.7.1. 在.NET 4.7.1中添加了HttpClientHandler.DefaultProxyCredentials属性。

  • 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. 最好的选择可能是针对最新的.NET版本,或至少4.7.1并使用您现在所使用的相同代码。 Better yet, target 4.7.2 to avoid the dependency shim hell explained in the next option. 更好的是,目标4.7.2以避免在下一个选项中解释的依赖性垫片。
  • You could just add the package. 你可以添加包。 It's the same .NET Standard 2.0 package used in .NET Core projects. 它与.NET Core项目中使用的.NET Standard 2.0包相同。 The downside to that is that 4.6.1 is not really .NET Standard 2.0 compatible and requires several compatibility libraries. 缺点是4.6.1 并不真正与.NET Standard 2.0兼容,并且需要多个兼容库。 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 .NET团队承认,尝试改进.NET Standard 2.0合规性是一个坏主意

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. 虽然NuGet认为.NET Framework 4.6.1支持.NET Standard 1.5到2.0,但是使用.NET Framework 4.6.1项目中为这些版本构建的.NET标准库存在一些问题。 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. 对于需要使用此类库的.NET Framework项目,我们建议您将项目升级到.NET Framework 4.7.2或更高版本。

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. 以及删除所有先前垫片的高级1天升级体验,以摆脱版本冲突。 Several issues indeed 确实Several issues

  • Set the Credentials property of the default proxy with 使用设置默认代理的Credentials属性
    WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

There is a ICredentials interface in the HttpClientHandler called Credentials. HttpClientHandler中有一个名为Credentials的ICredentials接口。 So: 所以:

using (var handler = new HttpClientHandler() 
{ Credentials = CredentialCache.DefaultCredentials }) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM