Scala/Play: javax.xml.soap request header Content-Type issue(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请求头内容-类型问题
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01