Scala/Play:javax.xml.soap请求头内容-类型问题

我在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对象会自动拥有一个值设置为"text/xml"的MIME标头Content-Type。该值必须为"text/xml",因为消息的SOAP部分中的内容必须是XML格式。非"text/xml"类型的内容必须在AttachmentPart对象中,而不是在SOAPPart对象中。

source

只需弄清楚如何更改我的代码以与此匹配。

UPDATE2:已解决只需更改1行即可指示这是SOAP 1.2:

val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)

解决方案

只需更改1行即可指示这是SOAP1.2,并自动设置正确的标头:

val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)

相关文章