加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 更改HttpClient的基地址

发布时间:2020-12-16 01:27:32 所属栏目:百科 来源:网络整理
导读:在编写使用HttpClient的应用程序时,我采用与 this post相同的方法,换句话说,我不使用使用静态HttpClient.当我只想与一台服务器通信时,我没有遇到任何问题. (我将Ip地址设置为BaseAddress并继续) 现在我遇到与this question相同的问题,因为在开始使用HttpClie
在编写使用HttpClient的应用程序时,我采用与 this post相同的方法,换句话说,我不使用使用静态HttpClient.当我只想与一台服务器通信时,我没有遇到任何问题. (我将Ip地址设置为BaseAddress并继续)

现在我遇到与this question相同的问题,因为在开始使用HttpClient后无法更改BaseAddress.

该问题的答案回答了一个无法完成的解释.您无法更改BaseAddress.

所以,如果我们想要更改ip地址以与其他服务器通信,我的问题(与链接的不同,所以不重复)是该怎么办?

我们应该实例化另一个HttpClient吗? (不,我们不打算使用)我们如何正确地从这里开始?

解决方法

就目前而言,您无法更改基址.

How do we correctly proceed from here?

不要设置基址,只需使用请求的完整地址.

这样,相同的客户端可以用于为每个基地址创建新客户端所需的所有其他请求,这也将破坏拥有单个客户端的优势.

asp.net core 2中的客户端工厂已经解决了与多个客户端相关的问题.

Disposing of the client is not mandatory,but doing so will cancel any ongoing requests and ensure the given instance of HttpClient cannot be used after Dispose is called. The factory takes care of tracking and disposing of the important resources that instances of HttpClient use,which means that HttpClient instances can be generally be treated as .NET objects that don’t require disposing.

One effect of this is that some common patterns that people use today to handle HttpClient instances,such as keeping a single HttpClient instance alive for a long time,are no longer required. Documentation about what exactly the factory does and what patterns it resolves will be available,but hasn’t been completed yet.

完成文件Use HttpClientFactory to implement resilient HTTP requests

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读