I have to set up an XML "web service" that receives a POST where the 'Content-type header will specify “text/xml”.'
What is the simplest way to get the XML into an XDocument
for access by VB.NET's axis queries?
I don't believe the web service is guaranteed to follow any protocol (e.g. SOAP, etc); just specific tags and sub-tags for various requests, and it will use Basic Authentication, so I will have to process the headers.
(If it matters:
* the live version will use HTTPS, and
* the response will also be XML.)
Given Steven's warning, the answer may be to parse
Request.InputStream
manually with Tom Holland's test first, followed byXDocument.Load
in thePage_Load
event.A Google search initiated before I asked the question, but only checked after, found this, also suggesting I'm on the right track.
Also I was going to ask the question implied by my point that the response had to be XML too, as to what is the best way for that, but I've found an answer here.
In summary, the final code is:
and the ASP.NET webpage.aspx is:
NB Throwing
HTTPException
is not a valid final solution for unwanted scenarios.I want to apologize in advance for not answering your question here, but I want to give a little warning. Perhaps it is already something you're taking into account, but if you don't take the appropriate counter measures, your system can be easily shut down using a denial of service attack, when processing XML from an unknown source (both over HTTP and HTTPS).
There is a technique called XML Entity Expansion attacks. Look for instance at this innocent looking peace of XML that will bring your server to its knees when it tries to process it:
This little XML document of less than 500 bytes will make your server try to allocate at least 160 GB of memory.
You can protect yourself against this by validating the incoming XML (with an DTD) before processing it.
You can read more information about this attack, here.
Good luck.
When you create a Web Service you define the format of the XML you will receive, and it is up to the sender to meet your format.
I usually mock up the information my web service will receive as
DataTables
in aDataSet
(since that closely mimics how I might store them in a database) then do aDataSet.getXML()
on my mocked upDataSet
(possibly also getting the default schema as well) to use as the template for the XML I am expecting to be "posted" to my web service.Then, when my web service receives a post, I can simply take the XML sent and use the
DataSet.readXML()
on the XML posted ... and deal with the information sent in theDataSet
.Most of my web service "return" values are the results of queries based on the information posted, so I do the same to format the return values ... get the results of my return queried data in a
DataSet
,DataSet.getXML()
.. and return it.