WebService returns 409 它似乎正在删除 encoding=utf-8 这怎么会发生

WebService returns 409 it seems to be dropping the encoding=utf-8 how could this happen

此 VBScript 将 XML 文档发布到网络服务:

    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

没有错误,看起来一切正常。长度正确,readyState 为 4,但 WS returns 为 HTTP 409。返回的自定义消息为 'inbound not ok encoding="UTF-8" is missing'

这意味着该节点缺少编码="utf-8"?然而它不是。它的 100% 在那里。 XML 看起来像这样:

    <?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>

这是来自 webService 人员的日志跟踪的开始。注意

    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>

可能是我的发送有问题,还是被 webService 丢弃了???非常坚持这一点。是发件人问题还是收件人问题?

编辑 - 我已经确定这是我的问题,而不是接收者。请注意将此添加到上面的代码中:

writelog(left(oXML.xml, 100))

它显示了我发送的 XML 的前 100 个字符。它是这样开始的: ?xml 版本="1.0"?
即使这样: ?xml version="1.0" encoding="utf-8"?

在 XML 文件中是 100%。这样做时如何删除它:

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

编辑:所以解决方案是 WhiteHat 的建议。它的实现是上面添加的七行代码,开始(包括)评论 'removes the head node and'

编码是有意去除的,因为 .xml 属性 returns 是一个 Unicode 字符串。

有关更多信息,请参阅 How to Encode XML Data@MSDN...

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."

如果您确定这是问题所在,您可以尝试将其手动添加到 xml 的其余部分。

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