我在Scala / Play应用程序中对SOAP API进行了这么简单的调用:
import javax.xml.soap._
object API {
def call = {
val soapConnectionFactory = SOAPConnectionFactory.newInstance
val soapConnection = soapConnectionFactory.createConnection
val url = "http://123.123.123.123"
val soapResponse = soapConnection.call(createSOAPRequest, url)
soapConnection.close
}
def createSOAPRequest = {
val messageFactory = MessageFactory.newInstance
val soapMessage = messageFactory.createMessage
val soapPart = soapMessage.getSOAPPart
val serverURI = "http://some.thing.xsd/"
val envelope = soapPart.getEnvelope
envelope.addNamespaceDeclaration("ecl", serverURI)
val soapBody = envelope.getBody
val soapBodyElem = soapBody.addChildElement("TestRequest", "ecl")
soapBodyElem.addChildElement("MessageID", "ecl").addTextNode("Valid Pricing Test")
soapBodyElem.addChildElement("MessageDateTime", "ecl").addTextNode("2012-04-13T10:50:55")
soapBodyElem.addChildElement("BusinessUnit", "ecl").addTextNode("CP")
soapBodyElem.addChildElement("AccountNumber", "ecl").addTextNode("91327067")
val headers = soapMessage.getMimeHeaders
headers.setHeader("Content-Type", "application/json; charset=utf-8")
headers.addHeader("SOAPAction", serverURI + "TestRequest")
headers.addHeader("Authorization", "Basic wfewefwefwefrgergregerg")
println(headers.getHeader("Content-Type").toList)
soapMessage.saveChanges
soapMessage
}
println
输出我设置的右Content-Type
标题:
List(application/soap+xml; charset=utf-8)
但我正在调用的远程SOAP API以415
响应:
Bad Response; Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
我已经检查了使用wireshark发送的请求,事实上,Content-Type
标头是错误的:
Content-Type: text/xml; charset=utf-8
为什么我设置的内容类型在这种情况下被忽略,我该怎么做才能修复它?
更新:我想我在这里做点什么:
SOAPPart对象是MIME部分,具有MIME标头Content-Id,Content-Location和Content-Type。由于Content-Type的值必须为“text / xml”,因此SOAPPart对象自动具有Content-Type的MIME标头,其值设置为“text / xml”。该值必须为“text / xml”,因为消息的SOAP部分中的内容必须是XML格式。不属于“text / xml”类型的内容必须位于AttachmentPart对象中,而不是SOAPPart对象中。
只需要弄清楚如何更改我的代码以匹配此内容。
UPDATE2:已解决只需更改1行即表示这是SOAP 1.2:
val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
答案 0 :(得分:0)
只需更改1行即可指示这是SOAP 1.2并自动设置右侧标题:
val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)