I am using the XDomainRequest object in IE8 and IE9 to do cross domain posting.  However, I hit an issue where I couldn't seem to get at the data I was posting: the Request.Form collection contained nothing yet I was clearly sending it. I then found this post:


which states: "we restricted the content type to text/plain but didn’t allow the caller to specify that the data was in application/x-www-urlencoded form. This is problematic because server-side frameworks (e.g. ASP, ASPNET, etc) will only automatically parse a request’s fields into name-value pairs if the x-www-urlencoded content type is specified.

To workaround this issue, server code that currently processes HTML Forms must be rewritten to manually parse the request body into name-value pairs when receiving requests from XDomainRequest objects. This makes adding support for the XDomainRequest object more difficult than it would be otherwise."

Okay, so how does one do that? Well, turned to be pretty simple. I just had to access the Request.InputStream directly, as follows:

           //set the header to support XDomainRequest

           Response.Headers.Add("Access-Control-Allow-Origin", "*");

           //now extract the data out of the stream

           System.IO.Stream str;

           String jsonContents;

           Int32 counter, strLen, strRead;

           // Create a Stream object.

           str = Request.InputStream;

           // Find number of bytes in stream.

           strLen = Convert.ToInt32(str.Length);

           // Create a byte array.

           byte[] byteArray = new byte[strLen];

           // Read stream into byte array.

           strRead = str.Read(byteArray, 0, strLen);

           // Convert byte array to a text string.

           jsonContents = Encoding.UTF8.GetString(byteArray);