Sending a 'application/soap+xml' SOAP requ

2019-04-01 01:19发布

问题:

Any help with this would be appreciated; I've been at it for a few days now.

Below is the code that I've got so far; unfortunatly when I run it I get a HTTP 415 error; Cannot process the message because the content type 'text/xml; charset=UTF-8' was not the expected type 'application/soap+xml; charset=utf-8'.

I have to send the content-type of application/soap+xml as this is the only type that the web service allows; and I have to do it in classic ASP.

I've tried changing the 'send' line to "objRequest.send objXMLDoc.XML" but this then gives me a HTTP 400 Bad Request error.


strXmlToSend = "<some valid xml>"
webserviceurl = "http://webservice.com"
webserviceSOAPActionNameSpace = "avalidnamespace"

Set objRequest = Server.createobject("MSXML2.XMLHTTP.3.0")
objRequest.open "POST", webserviceurl, False

objRequest.setRequestHeader "Content-Type", "application/soap+xml"
objRequest.setRequestHeader "CharSet", "utf-8"
objRequest.setRequestHeader "action", webserviceSOAPActionNameSpace & "GetEstimate"
objRequest.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "GetEstimate"

Set objXMLDoc = Server.createobject("MSXML2.DOMDocument.3.0")
objXMLDoc.loadXml strXmlToSend
objRequest.send objXMLDoc
set objXMLDoc = nothing

回答1:

When you pass an XML DOM ot the send method the Content-Type is always set to "text/xml".

If you want to control the content type then you must pass a string. Don't bother loading the XML string into a DOM only to call the xml property since that may change the content of the xml declaration. BTW what does the xml declaration look like in the XML string and are you certain that the xml is correct? The encoding on the xml declare if present should say "UTF-8".

Don't send a header CharSet it means nothing, CharSet is an attribute of the Content-Type header.

Don't use XMLHTTP from inside ASP its not safe.

Hence your code ought to look like this:-

strXmlToSend = "<some valid xml>" 
webserviceurl = "http://webservice.com" 
webserviceSOAPActionNameSpace = "avalidnamespace" 

Set objRequest = Server.Createobject("MSXML2.ServerXMLHTTP.3.0") 
objRequest.open "POST", webserviceurl, False 

objRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=UTF-8" 
objRequest.setRequestHeader "action", webserviceSOAPActionNameSpace & "GetEstimate" 
objRequest.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "GetEstimate" 

objRequest.send strXmlToSend 

Not sure about that "action" header either looks superflous to me. Perhaps this will still fail in some way but it shouldn't complain about the Content-Type header anymore.



回答2:

Here's what I've used successfully in the past:

    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    xmlhttp.open "POST", url, false
    xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
    xmlhttp.setRequestHeader "SOAPAction", "http://www.mydomain.com/myaction" 
    xmlhttp.send postdata
    xml = xmlhttp.responseText