C # HttpWebResponse Comet Problem

I am wondering how I will read the persistent connection with HttpWebRequest and HttpWebResponse. The problem is that the GetResponseStream () function waits for the connection to the server to complete before returning.

Is there an easy way to read a cometary connection? An example that does not work.

// get the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { // fill our buffer count = resStream.Read(buf, 0, buf.Length); // as long as we read something we want to print it if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Debug.Write(tempString); } } while (true); // any more data to read? 
+6
source share
1 answer

There is little reason to use HttpWebRequest if you can use WebClient . Take a look at the WebClient.OpenRead method . I successfully use it to read from an infinite HTTP response as follows:

 using (var client = new WebClient()) using (var reader = new StreamReader(client.OpenRead(uri), Encoding.UTF8, true)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } 

Note , however, that the โ€œlong pollโ€ point usually should not send a continuous stream of data, but rather to delay the response until any event occurs, in which case the response will be sent and the connection closed. So what you see, maybe Comet is working as intended.

+8
source

All Articles