C# : Dealing with HttpWebResponse timeout problems
Asked Answered
I

1

5

I have a big problem dealing with data I try to download in my Application over the internet via HttpWebResponse. My code looks like that:

myWebRequest.Timeout = 10000; 

using (HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
{
     using (Stream ReceiveStream = myWebResponse.GetResponseStream())
     {
         Encoding encode = Encoding.GetEncoding("utf-8");
         StreamReader readStream = new StreamReader(ReceiveStream, encode);
         // Read 1024 characters at a time.
         Char[] read = new Char[1024];

         int count = readStream.Read(read, 0, 1024);

         int break_counter = 0;
         while (count > 0 && break_counter < 10000)
         {
             String str = new String(read, 0, count);
             buffer += str;
             count = readStream.Read(read, 0, 1024);
             break_counter++;
         }
    }
}

This code runs in a few instances in separated threads so it's a little bit hard to debug. The problem is this method got stuck and I blame it on the poor connection to the data.

As you can see I already set a timeout and was hoping the code would just terminate after the timeout time has expired. It does not! At least not all the time. Sometimes I get a WebException/Timeout but a few times it just got stuck.

What is a timeout exactly? When is it called? Lets say the HttpWebResponse starts to receive data but it got stuck somewhere in the middle of transmission. Do I get a timeout? For me it looks like I don't because my application got stuck too and no timeout exception is raised.

What can I do to patch this up or how can I get further information about what is going wrong here?

Idelson answered 17/3, 2010 at 13:4 Comment(0)
M
15

Try setting HttpWebRequest.ReadWriteTimeout Property

The number of milliseconds before the writing or reading times out. The default value is 300,000 milliseconds (5 minutes).

Miran answered 17/3, 2010 at 13:10 Comment(3)
I made a test run and at the moment this looks very very good. Who sets default on 5 minutes?Idelson
I do not quiet understand your question. A default is a value, normally set when a variable is` initialized` (not in all cases), unless changed by user as in above sample.Miran
I mean who a Microsoft Framework developers thought 5 minutes would be a good idea? From my point of view it is insanity. I set it to 20 seconds and my application runs like a dream. Yes, i still get timesouts but I just try again later. Nothing is stuck anymore.Idelson

© 2022 - 2024 — McMap. All rights reserved.