本文介紹了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請求頭內容-類型問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,