Unable to set Content-Type on SOAP Request

2019-07-29 00:02发布

问题:

I'm sending a SOAP call to a server, and the server insists on a certain Content-Type. I'm trying to set the content type but it doesn't seem to be transmitted properly.

I get a response from the server:

415 Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'

My rough code:

val soapConnectionFactory = SOAPConnectionFactory.newInstance()
val soapConnection = soapConnectionFactory.createConnection()
val url = "https://secure.com"

val messageFactory = MessageFactory.newInstance()
val soapMessage = messageFactory.createMessage()

// (create envelope)

val headers = soapMessage.mimeHeaders
headers.setHeader("Content-Type", "application/soap+xml; charset=utf-8")

soapMessage.saveChanges()

val soapResponse = soapConnection.call(soapMessage, url)
soapConnection.close()

Debugging through, the content type appears to be set properly up to the soapConnection.call(). Any ideas?

回答1:

You are creating a default MessageFactory:

val messageFactory = MessageFactory.newInstance()

As doc states this method

Creates a new MessageFactory object that is an instance of the default implementation (SOAP 1.1)

SOAP 1.2 message content type is "application/soap+xml", whereas SOAP 1.1 is "text/xml" (http://www.w3.org/TR/soap12-part0/#L4697).

Try to create a message factory instance providing the SOAP 1.2 as protocol

val messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)


标签: java soap kotlin