I'm trying to post a simple XML document to a third party automatically, the content is a URL encoded XML document, the submission is currently being created using the following code:
Using client As New WebClient Dim reqParm As New NameValueCollection reqParm.Add("cxml-urlencoded", sXmlOrderMessage) Dim respBytes = client.UploadValues(cXMLSettings.SupplierSetupUrl, "POST", reqParm) Dim respBody = (New UTF8Encoding(False).GetString(respBytes)) End Using
This does work, and submit the contents needed. The XML is built using the XmlTextWriter component, the header of the document is as such:
Dim mem As New MemoryStream Dim writer As New XmlTextWriter(mem, Encoding.UTF8) writer.WriteStartDocument() writer.WriteDocType("cXML", Nothing, "http://xml.cXML.org/schemas/cXML/1.1.009/cXML.dtd", Nothing) writer.WriteStartElement("cXML") writer.WriteAttributeString("payloadID", objCXMLDetails.PayloadID + Request.Url.Host) writer.WriteAttributeString("xml:lang", "en-gb") writer.WriteAttributeString("timestamp", DateTime.Now.ToString("o"))
Once the XML is generated, I then use the following to convert it into a string:
writer.WriteEndDocument() writer.Flush() Dim reader As New StreamReader(mem) mem.Seek(0, SeekOrigin.Begin) Return reader.ReadToEnd
This returns a string, that I can then URL encode.
The XML return (I'm showing just the header), is this:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.1.009/cXML.dtd"> <cXML payloadID="20180305112030.15272.382530855@localhost" xml:lang="en-gb" timestamp="2018-03-05T11:20:30.9962738+00:00">
The problem is that on submission, I get the following error:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.017/cXML.dtd"> - <cXML timestamp="2018-03-05T08:46:58" payloadID="87f75924-9851-47c5-bd6d-76c723657476"> - <Response> <Status text="Not Acceptable org.jdom.input.JDOMParseException: Error on line 1: Content is not allowed in prolog." code="406" /> </Response> </cXML>
I've tried
- Removing the UTF-8 encoding from the XmlTextWriter.
- Removing the schema reference defined in the DocType.
- Tried using a regular expression to remove anything before the '<' at the beginning.
Any help at this point would be appreciated.
1 Answers
Answers 1
I've encountered this error elsewhere, as have others, and the problem is usually a character before the opening <, which you note you've tried to eliminate. You mentioned a regex, perhaps you could post that as well. There are many non-printing characters that can find their way into a stream, especially where a framework is doing the work for us.
Consider, what does the following line do, exactly?
writer.WriteStartDocument()
There may be some interplay with this and the MemoryStream that is unexpected. Perhaps there is something at the zero position of the MemoryStream when you create it?
Suggested fixes that I've seen usually involve dropping the generated XML to a file and examining the hex code to see what really leads off the file, perhaps a non printing character. This is what has helped me in the past as it provide a forensic approach to identifying the problem or confirming it exists elsewhere.
Some solutions advocate uploading a file rather than a stream or string, though I don't think this would be required if you can identify the exact polluting character sequence.
0 comments:
Post a Comment