Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Dealing with HttpWebResponse timeout problems

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?

like image 948
TalkingCode Avatar asked Mar 17 '10 13:03

TalkingCode


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


1 Answers

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

like image 184
Asad Avatar answered Nov 15 '22 02:11

Asad