views:

324

answers:

1

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?

+2  A: 

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).

Asad Butt
I made a test run and at the moment this looks very very good. Who sets default on 5 minutes?
Holli
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.
Asad Butt
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.
Holli