简体   繁体   中英

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. For some odd reason, the requests sometimes take a lot of time to complete (~10 minutes).

The HttpWebRequest object is not exposed by the library.

Is there any way to specify a global timeout and readwritetimeout for the requests, perhaps via app.config ?

Unfortunately not currently possible. The constructor of HttpWebRequest has this value hardcoded - reference source .

That timeout is in milliseconds - so 2000ms = only 2 seconds.

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>

Timeout = time spent trying to establish a connection (not including lookup time)

ReadWriteTimeout = time spent trying to read or write data after connection established

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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