17 January 2011

One of the developers I work with recently had a problem trying to make Web Service calls to a SOAP based Web method that requires Request Headers. I ran into the same problem about a year ago using CFMX 6.1. Request and response headers for web services are now built into CFMX 7. It is possible to get CFMX 6.1 to send request headers. Here is how you can do it with CFMX 6.1. The first thing you will have to do is make sure CFMX is patched to at least the 6.1 updater (6,1,0,83762) or the hf53566_61.jar hot fix. This patch will cause problems with trying to use cfdump to output the cfcatch variable, but there is also a patch for that as well.

You will need to use the createObject function to create a web service object instead of using cfinvoke to be able to use the request header.

<cfset WSObj = createObject("webservice","[https://www.somecompany.com/services/somews.asmx?wsdl](https://www.somecompany.com/services/somews.asmx?wsdl)") />

Request headers are usually in the form of a XML object. You can use the CFMX’s built features to create the xml object.

<cfset doc = XMLNew() /> <cfset doc.Authentication = XmlElemNew(doc, "[https://www.somecompany.com/services/](https://www.somecompany.com/services/) ", "Authentication") /> <cfset doc.Authentication.username = XmlElemNew(doc, "username") /> <cfset doc.Authentication.username.XmlText = "myUsername" /> <cfset doc.Authentication.password = XmlElemNew(doc, "password") /> <cfset doc.Authentication.password.XmlText = "myPassword" />

The resulting XML looks like this when serialized;

</font> </font>myUsername</username> </font>myPassword</password> </Authentication> </div> The object that is passed to to the request header method requires the use of some java, but it is very simple. The first thing that needs to happen is to turn the ColdFusion XML Object into something that Java will understand. There is a function built into every ColdFusion XML object that returns a Java based XML object.
<cfset jXMLdoc = doc.getDocumentElement() />
Once you have the java based XML object, you will need to create a SOAPHeaderElement java object that will be passed into the web service object.
<cfset headerElement = createObject("java","org.apache.axis.message.SOAPHeaderElement") /> <cfset headerElement.init(jXMLdoc) /> <cfset WSObj.setHeader(headerElement) />
Now that the request header has been set in the web service object, it is safe to call the methods that require that request header.
<cfset responseValue = WSObj.makeWSCall() />
I plan on creating a web service on my web site that use request and responce headers. Thanks to Tom Jordahl and Damon Cooper who showed me how to get this to work in ColdFusion 6.1. ColdFusion MX 7 handles this now quite well by using addSOAPRequestHeader() function. There is no longer a need to call the underlying java to make these calls.


blog comments powered by Disqus