lundi 5 mars 2018

Form Submission Error: Content is not allowed in prolog

I'm trying to post a simple XML document to a third party automatically, the content is a URL encoded XML document, the form for submission is currently being created using the following code:

            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8()
            HttpContext.Current.Response.Write("<html><head>")
            HttpContext.Current.Response.Write("</head><body onload=""document.cXML.submit()"">")
            HttpContext.Current.Response.Write(String.Format("<form name=""cXML"" method=""post"" action=""{0}"" >", cXMLSettings.SupplierSetupUrl))
            HttpContext.Current.Response.Write(String.Format("<input name=""cXML-urlencoded"" type=""hidden"" value=""{0}"">", sXmlOrderMessage))
            HttpContext.Current.Response.Write("</form>")
            HttpContext.Current.Response.Write("</body></html>")
            HttpContext.Current.Response.End()

This does work, and submit the contents of the input within the form. 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

  1. Removing the UTF-8 encoding from the XmlTextWriter.
  2. Removing the schema reference defined in the DocType.
  3. Tried using a regular expression to remove anything before the '<' at the beginning.

Any help at this point would be appreciated.

Aucun commentaire:

Enregistrer un commentaire