【问题标题】:How do I specify a global HttpWebRequest timeout?如何指定全局 HttpWebRequest 超时?
【发布时间】:2014-10-31 20:39:18
【问题描述】:

我正在使用一个 Twitter 库,该库在内部使用 HttpWebRequest 向 twitter API 发出请求。出于某种奇怪的原因,请求有时需要很长时间才能完成(约 10 分钟)。

库不公开HttpWebRequest 对象。

有没有办法为请求指定全局超时和 readwritetimeout,也许是通过 app.config

【问题讨论】:

    标签: c# winforms httpwebrequest tweetinvi


    【解决方案1】:

    很遗憾,目前不可能。 HttpWebRequest 的构造函数有这个值硬编码 - reference source

    【讨论】:

      【解决方案2】:

      超时以毫秒为单位 - 所以 2000 毫秒 = 仅 2 秒。

      System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("URL");
      req.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["timeOut"]);
      Req.ReadWriteTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["readWriteTimeout "]);
      

      App.config

      <appSettings>
      <add key="timeOut" value="200" />
      <add key="readWriteTimeout " value="10000" />
      </appSettings>
      

      超时 = 尝试建立连接所花费的时间(不包括查找时间)

      ReadWriteTimeout = 建立连接后尝试读取或写入数据所花费的时间

      【讨论】:

      • 谢谢,但正如问题中所述,我无权访问 req 对象。
      • 由于在 web.config 中是不可能的,所以这个答案有助于解决问题。谢谢!
      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多