[英]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属性。
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
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.