【问题标题】:HttpClient's equivalent of HttpWebRequest's ServicePoint.MaxIdleTime propertyHttpClient 等效于 HttpWebRequest 的 ServicePoint.MaxIdleTime 属性
【发布时间】:2016-09-26 15:05:25
【问题描述】:

我正在将旧的 .NET 代码转换为 .NET 4.5 框架。

我正在更新的一个特定方法是将HttpWebRequest 更改为HttpClient 调用。现有代码调用外部 Web API。

这相当简单,只是现有 HttpWebRequest 代码中的代码为请求的ServicePoint.MaxIdleTime 分配了一个值。对于 HttpClient 来说,这相当于什么。

编辑
以下是HttpWebRequest 的设置方式。

var webReq = WebRequest.Create(API_ENDPOINT) as HttpWebRequest;

webReq.Method = "POST";
webReq.ContentType = "text/xml; charset=utf-8";
webReq.ServicePoint.MaxIdleTime = 1000 * 10; 

【问题讨论】:

  • 完全一样。 ServicePoint.MaxIdleTime 是影响两个类的“全局”设置
  • @PanagiotisKanavos 我刚刚更新了我的问题。好像是在 HttpWebRequest 的实例上设置的?
  • Chrome 不允许我删除之前的评论。 HttpClient 在下面使用 HttpWebRequest,因此您应该能够使用 ServicePointManager.FindServicePoint 获取 URL 的 ServicePoint

标签: .net httpwebrequest dotnet-httpclient


【解决方案1】:

您可以使用全局静态类ServicePointManager 来设置ServicePoint 空闲限制。您还可以使用FindServicePoint(uri) 方法获取对单个ServicePoint 的引用,以便在初始调用后进行修改。或者,您可以使用组

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多