2

This VBScript posts an XML document to a webService:

    Dim xmlhttp, oXML, sourceURL, WSURL, WSUserName, WSPassword, XMLResponse
    sourceURL = "C:\temp\myFileName.xml"
    WSURL = "https://mywebServiceURL"
    WSUserName = "myUserName"
    WSPassword = "myPassword"

    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")   
    Set oXML = CreateObject("Microsoft.XMLDOM")    
    oXML.load(sourceURL) 

    xmlhttp.open "POST", WSURL, false, WSUserName, WSPassword   
    xmlhttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded; charset=UTF-8"   
    xmlhttp.setRequestHeader "Content-length", Len(oXML.xml)   
    xmlhttp.setRequestHeader "Connection", "close"   
    xmlhttp.setRequestHeader "soapAction", "processRequest"   
    'removes the head node and replaces it with a node which forces utf-8.  
    'Setting encoding headers simply does not work!!
    dim repText, sXML, fXML 
    repText = "<?xml version=""1.0""?>"
    fXML = replace(oXML.xml, repText, "")
    sXML = "<?xml version=""1.0"" encoding=""utf-8""?>"
    xmlhttp.send(sXML & fXML)

    writeLog "XML len = " & Len(sXML & fXML)
    writeLog "readyState = " & xmlhttp.readyState   

    If(xmlhttp.readyState = 4) then   
        WSStatus = xmlhttp.status
        writeLog "WSStatus = " & WSStatus
        WSResponse = xmlhttp.responseText   
        writeLog "WSResponse = " & WSResponse
        WSResponse = xmlhttp.responseBody 
        writeLog "WSResponseBody = " & WSResponseBody
    else
        writeLog "ERROR : readyState = " & xmlhttp.readyState & " : "
    end if

There are no errors, all appears to be fine. The length is correct, the readyState is 4 however the WS returns an HTTP 409. The custom message returned is 'inbound not ok encoding="UTF-8" is missing'

This implies that this node is missing the encoding="utf-8"? however its not. Its 100% there. The XML looks like so:

    <?xml version="1.0" encoding="utf-8"?>
    <ORDERS05>
      <IDOC BEGIN="1">
        <EDI_DC40 SEGMENT="1">
          <TABNAM><![CDATA[EDI_DC40]]></TABNAM>
          <IDOCTYP>aaa</IDOCTYP>
          <MESTYP>sss</MESTYP>
          <SNDPOR>ddd</SNDPOR>
        </EDI_DC40>
      </IDOC>
    </ORDERS05>

Here is the start of the log trace from the webService people. Note the

    POST /dir/anotherDir HTTP/1.1
    content-type: application/x-www-form-urlencoded; charset=UTF-8
    connection: close
    soapaction: processRequest
    accept-language: en-au
    content-length: 4699
    accept: */*
    user-agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
    host: test.aURLHere.co.nz
    clientprotocol: https
    ssl_cipher_usekeysize: 128
    ssl_cipher_suite: 0005
    <?xml version="1.0"?>
    <ORDERS05>
     <IDOC BEGIN="1">
      <EDI_DC40 SEGMENT="1">
       <TABNAM><![CDATA[EDI_DC40]]></TABNAM>
       <IDOCTYP>aaa</IDOCTYP>
       <MESTYP>bbb</MESTYP>
       <SNDPOR>ccc</SNDPOR>

Is is possible my sending has the issue, or could this be being dropped by the webService??? Very stuck on this one. Is it the senders issue or the receiver?

EDIT - I have identified that this is my Issue not the recievers. Note this added to the code above:

writelog(left(oXML.xml, 100))

it shows me the first 100 chars of the XML I am sending. It starts with this: ?xml version="1.0"?
even though this: ?xml version="1.0" encoding="utf-8"?

is 100% in the XML file. How is this being dropped just when doing this:

Set oXML = CreateObject("Microsoft.XMLDOM") 
oXML.load(sourceURL) 

EDIT : So the solution is WhiteHat's suggestion. Its implementation is the seven lines of code added above starting (and including) the comment 'removes the head node and'

2
  • were you able to resolve?
    – WhiteHat
    Dec 6, 2015 at 0:41
  • Yes thanks to your post. I try to stay 100 off-line in the w-ends these days. Please fins my updates to the OP and below. Im not sure appending and then find/replace is the most elegant solution but it does work. Thanks again
    – Mat41
    Dec 7, 2015 at 2:04

1 Answer 1

0

The encoding is stripped on purpose because the .xml property returns a Unicode string.

See How to Encode XML Data @ MSDN for more...

Now, be careful you don't let the XML property confuse you. The XML property returns a Unicode string. If you call the XML property on the DOMDocument object after creating the ISO-8859-1 encoding declaration, you will get the following Unicode string back:

<?xml version="1.0"?>
<test>a</test>

Notice that the ISO-8859-1 encoding declaration is gone. This is normal. The reason it did this is so that you can turn around and call LoadXML with this string and it will work. If it does not do this, LoadXML will fail with the error message: "Switch from current encoding to specified encoding not supported."

If you're sure that is the problem, you can try adding it manually to the rest of the xml.

"<?xml version=""1.0"" encoding=""utf-8""?>" & oXML.DocumentElement.XML

1
  • Thank you. This did lead me to the reason/solution. It used your suggestion of adding it but also has to find/repalce the XML docs own node where the encoding was dropped from. I have commented and included the code in the OP. Thank again whiteHat, a very good day to you
    – Mat41
    Dec 7, 2015 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.