HttpWebRequest's Timeout and ReadWriteTimeout -- What do these mean for the underlying TCP connection?

16,013

Solution 1

This problem has been debated on another question, see Adjusting HttpWebRequest Connection Timeout in C#. The discussion made my head spin, so I'll offer my summary.

Although, MSDN explains that the HttpWebRequest.Timeout Property applies to HttpWebRequest.GetResponse and HttpWebRequest.GetRequestStream calls, the description is a bit confusing.

Jim Mischel is more helpful: Timeout "is the time for the server to respond to a request, not the amount of time to wait for the server to respond and send down all of the data." Thus, Timeout covers establishing a working connection. For large payloads, this does not imply that the request/reply is complete.

ReadWriteTimeout applies to Read or Write operations to streams that transmit over the connection. E.g. when you Write to the stream returned by GetRequestStream. The connection is already established, but there is a risk that it will break. E.g. the network connection goes down.

The Jim Mischel link has some very good advice on what values to set these timeout. I.e. the default for ReadWriteTimeout is too long.

Solution 2

.Timeout = time spent trying to establish a connection (not including lookup time) .ReadWriteTimeout = time spent trying to read or write data after connection established

Share:
16,013
Mark Rolich
Author by

Mark Rolich

Self-taught programmer. B.Sc. Mathematics at UCLA (2006) and M.Sc. Computer Science at UCSD (2013). My other interests include economics, philosophy, and linguistics.

Updated on June 05, 2022

Comments

  • Mark Rolich
    Mark Rolich almost 2 years

    I believe I understand the practical differences between HttpWebRequest.Timeout and HttpWebRequest.ReadWriteTimeout. However, I'm seeking further clarity on the difference between these two timeouts, including what these values mean with respect to the underlying TCP connection/buffers/state if applicable.

    For instance, are these timeouts used only during the initialization of the TCP connection, or are these only managed values for keeping watch on the unmanaged connection?

    What are the client-server scenarios in TCP terms where each of these timeouts would apply or not apply?

  • Tim Lewis
    Tim Lewis over 8 years
    Another tidbit of information: HttpWebRequest.ReadWriteTimeout works by setting the underlying NetworkStream's WriteTimeout and ReadTimeout properties (learned by disassembling System.Net).
  • dstj
    dstj over 7 years
    A thing that's worth mentioning is that Timeout encompasses the ReadWriteTimeout. If you have Timeout < ReadWriteTimeout, long running request may still timeout. I misunderstood that the first time...
  • codewarrior
    codewarrior almost 7 years
    @dstj default value of timeout = 100s while ReadWriteTimeout = 300s. So by default Timeout < ReadWriteTimeout.
  • Simon Tewsi
    Simon Tewsi almost 7 years
    Jim Mischel's article seems to have been removed from informit.com. The Wayback Machine has it archived, though: web.archive.org/web/20160523140506/http://www.informit.com/…