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

Scala/Play: javax.xml.soap request header Content-Type issue(Scala/Play:javax.xml.soap请求头内容-类型问题)

本文介绍了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)

这篇关于Scala/Play:javax.xml.soap请求头内容-类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐