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

标签 java scala soap playframework playframework-2.0

我在我的 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 header :

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 header 是错误的:

Content-Type: text/xml; charset=utf-8

为什么在这种情况下我设置的内容类型被忽略了,我该怎么做才能解决这个问题?

更新:我想我正在做一些事情:

SOAPPart 对象是一个 MIME 部分,具有 MIME header Content-Id、Content-Location 和 Content-Type。因为 Content-Type 的值必须是“text/xml”,所以 SOAPPart 对象自动具有 Content-Type 的 MIME header ,其值设置为“text/xml”。该值必须是“text/xml”,因为消息的 SOAP 部分中的内容必须是 XML 格式。非“text/xml”类型的内容必须在 AttachmentPart 对象中,而不是在 SOAPPart 对象中。

source

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

UPDATE2:已解决只需更改 1 行以表明这是 SOAP 1.2:

val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)

最佳答案

只需要更改 1 行以指示这是 SOAP 1.2 并自动设置正确的标题:

val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)

关于java - Scala/Play : javax. xml.soap 请求 header 内容类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249681/

相关文章:

c# - 从 C# 调用 soap 服务

java - 在 java.nio 中指定连接超时

java - Windows 批处理 & java : Combining -XX:OnOutOfMemoryError command and batch parameters

eclipse - 如何在本地构建和运行 Scala Spark

scala - 如何在indexWhere向量表达式中捕获内部匹配值?

soap - JAXB - 将 SOAP 映射到 Java 类

java - Hadoop 递归映射

Java Jsoup 网页抓取

scala - 如何在丢弃集合的某些元素时进行分组

ios - 使用 SOAP 网络服务时出现 "No Action header was found"错误消息