C# HttpWebResponse Comet problem

前端 未结 1 1878
耶瑟儿~
耶瑟儿~ 2021-01-05 11:55

I am wondering how I would go about reading a persistent connection with HttpWebRequest and HttpWebResponse. The problem seems to be that the GetResponseStream() function wa

相关标签:
1条回答
  • 2021-01-05 12:43

    There is little reason to use HttpWebRequest if you can use WebClient instead. Have a look at the WebClient.OpenRead Method. I'm successfully using it to read from an infinite HTTP response like this:

    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 point of "long-polling" is usually not to send a continuous stream of data, but to delay the response until some event occurs, in which case the response is sent and the connection closed. So what you're seeing might simply be Comet working as intended.

    0 讨论(0)
提交回复
热议问题