Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve XML from HTTP POST Request Payload

I have an ActiveX that POSTs to the server (HTTP Handler) with a payload of a certain XML document.

Is there a better way to retrieve the payload into XML than the below?

private static byte[] RequestPayload()
{
   int bytesToRead = HttpContext.Current.Request.TotalBytes;
   return (HttpContext.Current.Request.BinaryRead(bytesToRead));
}

using (var mem = new MemoryStream(RequestPayload()))
{
    var docu = XDocument.Load(mem);
}

Once I have the "docu" I can query using LINQ to XML.

Thanks

like image 608
Bill Avatar asked Mar 17 '26 11:03

Bill


1 Answers

Simply load the XML from the InputStream of the Request e.g.

XDocument doc;
using (Stream input = HttpContext.Current.Request.InputStream)
{
  doc = XDocument.Load(input);
}

there is no need for a MemoryStream in my view.

like image 58
Martin Honnen Avatar answered Mar 19 '26 11:03

Martin Honnen