Encoding “<” and “>” while sending XML via HTTP

2019-08-10 22:56发布

问题:

I am sending an XML content via HTTP Post from Access VBA to Web Methods, using XMLHTTP object in MSXML. Here is the Code.

Dim objXmlHttp As Object
Set objXmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
objXmlHttp.Open "POST", webServicePath, False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

Dim Response As String
objXmlHttp.send wrt.output

'OK status
If objXmlHttp.Status = 200 Then
    Response = objXmlHttp.responseText
End If
Set objXmlHttp = Nothing

I am getting the XML with "&lt" and "&gt" instead of < and >. If I try to do URL encoding, everything is received as ASCII text in the Recipient side. Can you please guide what I need to do to get the valid XML format.

回答1:

You need to set the content-type correctly, try this instead:

objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=""utf-8"""