What is the proper way to read multiple Http Responses from a single Http Request (C# / .NET5 )

285 views Asked by At

I am trying to connect to an existing Service that listens for POSTS and responds with data. Please note: The existing service has multiple versions and is out in the field, so modifications to the Service itself is not desirable.

When I POST (HTTP/1.1) a <Get_Data> request, the Service will respond in one of two ways: Scenario 1: The requested data is small enough to service in the current Request/Receive cycle and I get back the values I expect in my request stream.

Scenario 2: The requested data is too large to immediately send back, and I will get N number of "Progress_Info" packages spaced about 4s apart before getting back the real payload.

When I 'WireShark' the communication with an existing C++ program that I am trying to replicate in C#, I get the following description of the interim packets:

  • Content-Length: 102 \n
  • HTTP response 3/4
  • [Prev response in frame: 14]
  • [Next response in frame: 25]

So, the real question is - how do I keep the response stream open, receive (and discard) the interim "Progress_Info" messages, until I get (and keep) the final message that contains the desired data.

This is what I have as the send/receive code:

public string Send_SOAP_Request_And_Receive_Response( string iInterface, string iRequestMessage )
{
   string oResponseText = string.Empty;
   try
   {
      HttpWebRequest request = Create_Message( MESSAGE_TYPE_POST, iInterface );
      byte[] bytes;
      bytes = System.Text.Encoding.ASCII.GetBytes( iRequestMessage );
      request.ContentLength = bytes.Length;
      Stream requestStream = request.GetRequestStream();
      requestStream.Write( bytes, 0, bytes.Length );
      HttpWebResponse response = ( HttpWebResponse )request.GetResponse();
      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader( responseStream );  
      if ( response.StatusCode == HttpStatusCode.OK )
      {
          oResponseText = reader.ReadToEnd();
          // MessageBox.Show( oResponseText );
      }
      // *NEED CODE TO CONTINUE READING THE STREAM UNTIL REAL MESSAGE IS SENT* //
      requestStream.Close();
   }
   catch (Exception ex)
   {
      Global.Display_Message( ex.Message );
   }
   return oResponseText;
}
0

There are 0 answers