公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了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请求头内容-类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Javax Play scala SOAP 内容 类型 请求
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定