gpt4 book ai didi

c# - 在 HttpClientHandler 可移植类库 (PCL) 中设置代理时找不到方法

转载 作者:行者123 更新时间:2023-11-30 22:15:23 29 4
gpt4 key购买 nike

我正在使用 Microsoft HTTP Client Libraries - PCL (通过 NuGet 安装)与公共(public) REST API 通信。因为我在公司防火墙后面,所以我需要在 HttpClientHandler 上配置代理信息。

但是,当在 HttpClientHandler 上设置代理时,会抛出以下错误:

Method not found: 'Void System.Net.Http.HttpClientHandler.set_Proxy(System.Net.IWebProxy)'.

我的代码没有什么特别之处,所以有点疑惑:

var handler = new HttpClientHandler
{
UseDefaultCredentials = false,
Proxy = new DefaultProxy
{
Credentials =
new NetworkCredential(
"firstname.lastname",
"P4ssw0rd",
"DOMAIN")
},
UseProxy = true
};

this.client = new HttpClient(handler);

一些注意事项:

  • 我已经成功地将来自标准 .Net 库(即非 PCL)的 HttpClientHandler 用于另一个项目,因此我知道代理信息是正确的。
  • 我已在绕过代理时成功检索到数据(即在公司网络外部,或没有代理)。
  • DefaultProxy 实现 IWebProxy。

最佳答案

这个错误是由于 HttpClient 包没有安装在消费项目上。您应该会收到类似的警告:

所有引用 [consuming project] 的项目都必须安装 nuget 包 Microsoft.Bcl.Build。

基本上,每个引用引用 HttpClient 和 Async 的类库也必须引用 HttpClient 和 Async。

关于c# - 在 HttpClientHandler 可移植类库 (PCL) 中设置代理时找不到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988628/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com