HttpRequest body question

Mar 1, 2010 at 1:51 PM
Edited Mar 1, 2010 at 2:28 PM

I'm experiencing a strange problem in a HttpModule. When using Curl (http://curl.haxx.se) to POST data to my application then request.GetBody() returns an empty array. However, if I perform a request.Body.Seek(0, SeekOrigin.Begin) first then the correct data is returned.

My Windows C++ and C# clients do not require the Seek() first.

Also I have two customers complaining about my application reporting:

System.ObjectDisposedException: Cannot access a closed Stream.

   at System.IO.__Error.StreamIsClosed()

   at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)

   at MyApp.Process(IHttpRequest request, IHttpResponse response, IHttpSession session)

The two customers are using clients written in Java and Oracle Stored Procedures. Unfortunately I have not been able to get access to these clients so I can debug the webserver.

My HttpModule is _very_ simple:

 

public override bool Process(IHttpRequest request, IHttpResponse response, IHttpSession session)
{
  ASCIIEncoding enc = new ASCIIEncoding();

  try
  {
    if (request.Uri.AbsolutePath != MYPATH)
      return false;
    else
    {
      request.Body.Seek(0, SeekOrigin.Begin);
      byte[] body = request.GetBody();
      MemoryStream bodyStream = new MemoryStream(body);
      byte[] rsp = null;
      if (request.ContentLength != 0)
      {
if (request.ContentLength != 0)
        XmlDocument dom = new XmlDocument();
        dom.Load(bodyStream);
      
        // Process xml (removed)
      }
      else
      {
        // rsp = error message
      }
      // Send response
      response.AddHeader("content-type", "text/xml");
      StreamWriter writer = new StreamWriter(response.Body);
      writer.Write(enc.GetString(rsp));
      writer.Flush();
  }
  catch (Exception ex)
  {
    // Handle exception
  }
}

 

 

Coordinator
Mar 2, 2010 at 8:58 AM

I don't remembers the details about GetBody, but try this:

 

request.Body.Seek(0, SeekOrigin.Begin);
byte[] body = new byte[request.Body.Length];
request.Body.Read(body, 0,  request.Body.Length);


        
    
Mar 3, 2010 at 8:44 PM

1. Is it a design decision that Seek() must be the first call? Anyway, this is not a big problem. But...

2. Two customers are complaining about the exception which is about the stream being disposed when I call seek(). Has anyone else encountered this?