简体   繁体   English

如何指定全局HttpWebRequest超时?

[英]How do I specify a global HttpWebRequest timeout?

I'm using a twitter library that uses HttpWebRequest internally to make requests to the twitter API. 我正在使用一个Twitter库,该库在内部使用HttpWebRequest向Twitter API发出请求。 For some odd reason, the requests sometimes take a lot of time to complete (~10 minutes). 由于某些奇怪的原因,请求有时会花费很多时间(〜10分钟)来完成。

The HttpWebRequest object is not exposed by the library. 库未公开HttpWebRequest对象。

Is there any way to specify a global timeout and readwritetimeout for the requests, perhaps via app.config ? 是否有可能通过app.config为请求指定全局超时和readwritetimeout的方法?

Unfortunately not currently possible. 不幸的是目前无法实现。 The constructor of HttpWebRequest has this value hardcoded - reference source . HttpWebRequest的构造函数具有此值的硬编码参考源

That timeout is in milliseconds - so 2000ms = only 2 seconds. 超时时间以毫秒为单位-因此2000ms =仅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 App.config

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

Timeout = time spent trying to establish a connection (not including lookup time) 超时=尝试建立连接所花费的时间(不包括查找时间)

ReadWriteTimeout = time spent trying to read or write data after connection established ReadWriteTimeout =建立连接后尝试读取或写入数据所花费的时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM