我必須在我的斯卡拉一個SOAP API這個簡單的通話/播放應用:斯卡拉/播放:javax.xml.soap中的請求頭Content-Type的問題
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-Type。因爲Content-Type的值必須是「text/xml」,所以SOAPPart對象自動具有Content-Type的MIME頭,其值設置爲「text/xml」。該值必須是「text/xml」,因爲消息的SOAP部分中的內容必須是XML格式。不是「text/xml」類型的內容必須位於AttachmentPart對象中,而不是SOAPPart對象中。
只需要弄清楚如何更改我的代碼以符合這一點。
UPDATE2:解決只是需要改變1行來指示,這是SOAP 1.2:
val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)