0

I am trying to handle an SSE using HttpWebRequest

    private async Task<string> WaitForQueuePass(string bearer, string token)
    {
        string url = $"https://blah.com";
        using (var client = new HttpClient())
        {
            // Add headers to indicate SSE response
            client.Timeout = TimeSpan.FromHours(2); //exaggerated for testing purposes
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("authorization", $"Bearer {bearer}");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream"));

            // Send GET request to SSE endpoint
            using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
            {
                response.EnsureSuccessStatusCode();
                
                // Process SSE stream
                using (var stream = await response.Content.ReadAsStreamAsync())
                {
                    stream.ReadTimeout = 6000 * 10; //10 min
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                    {
                        string data = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        if (data.Contains("passingToken"))
                        {
                            return StringExtensions.Split(data, "\"passingToken\":\"")[1].Split("\"")[0];
                        }
                    }
                }
            }
        }

        return "";
    }

The issue I'm having is that the server takes a few minutes to send the desired message. I've debugged my code and stream.ReadAsync is finishing/timing out after 60s, the connection closes and I never get the desired message.

I am using .NET framework 4.8

How can I make the stream reader continue to wait until my desired message is received?

Sam
  • 602
  • 9
  • 21
  • 1
    set the [Timeout](https://learn.microsoft.com/en-us/dotnet/api/system.net.webrequest.timeout?view=net-7.0) to an accepted value (of more then a few minutes) ? – Luuk May 30 '23 at 14:07
  • 1
    Are you using .NET Framework or .NET Core? In .NET Core (which includes .NET 5+) HttpWebRequest is nothing more than a legacy wrapper over HttpClient. Most of the code could be replaced with `HttpClient.GetStreamAsync` – Panagiotis Kanavos May 30 '23 at 14:07
  • Try to use ```Retry-pattern```. – Nexo May 30 '23 at 14:08
  • Retry pattern won't work. Retrying to listen to the SSE causes it to send the events from the start (and therefore I run into the same problem.= – Sam May 30 '23 at 14:55
  • check for end of stream is incorrect, see: https://stackoverflow.com/questions/13650668/textreader-peek-behaviour-and-detecting-end-of-stream-reader and https://social.msdn.microsoft.com/Forums/vstudio/en-US/4a97732d-fabf-4317-ab82-900f4dfc7929/need-help-stream-reader-says-its-at-the-end-of-stream-but-there-are-still-more-lines-i-want-it-to?forum=csharpgeneral and https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=net-7.0#examples – Luuk May 30 '23 at 14:57
  • Do not use `WebRequest`. Use `HttpClient`. – Ian Kemp May 30 '23 at 15:06
  • Updated to use `HttpClient`, still have the same issue – Sam May 30 '23 at 15:37
  • While there's a chance you will be able to solve this problem in your code, there is always a chance that a firewall, proxy server, or other timeout not under your control will get in your way on the network. Is the server yours or is it third party? – John Wu May 30 '23 at 16:51

0 Answers0