0

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;
}
  • 5
    That doesn’t sound like valid http – Daniel A. White Jul 11 '22 at 13:36
  • There are no multiple responses in HTTP. BTW in .NET Core HttpWebRequest is only a legacy wrapper over HttpClient. What you wrote is a verbose and buggy (due to ASCII.GetBytes and incorrect stream handling) way of writing `var response=await client.PostAsync(url,new StringContent(msg));` – Panagiotis Kanavos Jul 11 '22 at 13:40
  • 2
    Are multiple packages HTTP or some other protocol? HTTP you get only one response for each request. The response may come in multiple chunks, but the chunks are still part of the same response. A HTTP Request/Response uses TCP as low level protocol. The sniffer will automatically combine the TCP messages together into a single Request or response. – jdweng Jul 11 '22 at 13:43
  • The code you posted already reads everything to the end. The response stream closes because the server closed it. If what you claim is true, that server isn't using HTTP. We can't guess what it uses from your description – Panagiotis Kanavos Jul 11 '22 at 13:43
  • What does the actual response look like? Where did `HTTP response 3/4` come from? Is that part of the response or your interpretation of Wireshark packets? Have you tried the same with POSTMAN or [sending an HTTP POST from Chrome](https://stackoverflow.com/questions/14248296/making-http-requests-using-chrome-developer-tools) ? – Panagiotis Kanavos Jul 11 '22 at 13:48

0 Answers0