0

I have created a simple XML Request on test.aspx page.

System.Net.WebRequest req = System.Net.WebRequest.Create("http://server.loc/rq.aspx");

            req.ContentType = "text/xml";
            req.Method = "POST";

            string strData = "<root><test>test1 </test></root>";
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strData);
            req.ContentLength = bytes.Length;
            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);


            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

            string responsecontent = sr.ReadToEnd().Trim();

Now, on rq.aspx I want to anticipate webrequest and generate some kind of response based on strData. I really don't know how to access strData from web-request.

mko
  • 6,638
  • 12
  • 67
  • 118

1 Answers1

1

This is probably what you are looking for

private void Page_Load(object sender, EventArgs e)
{
    // Read XML posted via HTTP
    using (var reader = new StreamReader(Request.InputStream))
    {
        string xmlData = reader.ReadToEnd();
        // do something with the XML
    }
}

From this answer

Community
  • 1
  • 1
Ric
  • 8,615
  • 3
  • 17
  • 21
  • Yes! Is this the only way to handle requests containing xml data? – mko Feb 21 '13 at 19:38
  • Probably not but you can then parse into an XMLDocument and use its library. XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlData); http://stackoverflow.com/questions/1238528/parse-xml-document-in-c-sharp – Ric Feb 21 '13 at 20:00
  • Just one question. Where is this input stream being stored in request? As a part of header? – mko Feb 22 '13 at 09:07
  • As part of the body of the request after the headers. You can investigate further with something like [Fiddler](http://www.fiddler2.com/fiddler2/) which lets you view the raw requests and responses. – Ric Feb 25 '13 at 22:15