更改 HttpClient 的基地址
Changing the Base Address of a HttpClient
在编写使用 HttpClient 的应用程序时,我使用与 this post 相同的方法,换句话说,我不使用 using,而是使用静态 HttpClient。当我只想与一台服务器通信时,我没有遇到任何问题。 (我将 IP 地址设置为 BaseAddress
并继续)
现在我遇到了与相同的问题,即开始使用HttpClient 后无法更改BaseAddress。
该问题的答案回复了无法完成的解释。您不能更改 BaseAddress。
所以我的问题(与链接的不同,因此不是重复的)是如果我们想更改 ip 地址以与其他服务器通信怎么办?
我们是否应该实例化另一个 HttpClient? (不,我们不会使用 using
)我们如何正确地从这里开始?
目前您不能更改基地址。
How do we correctly proceed from here?
不设置基地址,只使用请求的完整地址。
这样同一个客户端可以用于所有请求,否则您需要为每个基地址创建一个新客户端,这也会破坏拥有单一客户端的优势。
asp.net 核心 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
在编写使用 HttpClient 的应用程序时,我使用与 this post 相同的方法,换句话说,我不使用 using,而是使用静态 HttpClient。当我只想与一台服务器通信时,我没有遇到任何问题。 (我将 IP 地址设置为 BaseAddress
并继续)
现在我遇到了与
该问题的答案回复了无法完成的解释。您不能更改 BaseAddress。
所以我的问题(与链接的不同,因此不是重复的)是如果我们想更改 ip 地址以与其他服务器通信怎么办?
我们是否应该实例化另一个 HttpClient? (不,我们不会使用 using
)我们如何正确地从这里开始?
目前您不能更改基地址。
How do we correctly proceed from here?
不设置基地址,只使用请求的完整地址。
这样同一个客户端可以用于所有请求,否则您需要为每个基地址创建一个新客户端,这也会破坏拥有单一客户端的优势。
asp.net 核心 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