How to form a cfhttp call to consume a custom webservice API
I've been a cf developer for 11 years, but embarrassed to say that I've done nothing substantial with webservices.
How to I form a cfhttp call to consume the following webservice API provided by the vendor?
Soap 1.2 Request:
POST /Portal/internet.asmx HTTP/1.1
Host: 192.168.222.240
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<开发者_JS百科Usage xmlns="http://portal/internet.asmx">
<SessionID>string</SessionID>
<CustomerCode>int</CustomerCode>
<FullUserName>string</FullUserName>
<StartDate>dateTime</StartDate>
<EndDate>dateTime</EndDate>
</Usage>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
Soap 1.2 Response:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UsageResponse xmlns="http://portal/internet.asmx">
<UsageResult>
<xsd:schema>schema</xsd:schema>xml</UsageResult>
</UsageResponse>
</soap12:Body>
</soap12:Envelope>
I want to do it manually at the moment (I know about cfinvoke and createobject). I came up with the following from a Ben Nadel blog, but I get a "connection failure" error. I guess I just need someone to check for obvious flaws in the code before I look at whether it is genuinely connection/authorisation related.
<cfsavecontent variable="soapBody">
<cfoutput>
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Usage xmlns="http://portal/internet.asmx">
<SessionID>F7B3B3FB-DE35-45CB-A785-8229E91FAEC9</SessionID>
<CustomerCode>1112221</CustomerCode>
<FullUserName>MR DAVE GEORGE</FullUserName>
<StartDate>2010-01-01</StartDate>
<EndDate>2009-01-01</EndDate>
</Usage>
</soap:Body>
</soap:Envelope>
</cfoutput>
</cfsavecontent>
<cfhttp
url="http://portal/internet.asmx"
method="post"
result="httpResponse">
<cfhttpparam
type="header"
name="SOAPAction"
value="http://portal/internet.asmx/Usage"
/>
<cfhttpparam
type="header"
name="accept-encoding"
value="no-compression"
/>
<cfhttpparam
type="xml"
value="#trim( soapBody )#"
/>
</cfhttp>
<cfoutput>
#httpResponse.fileContent# <!--- ouputs "connection failure" --->
</cfoutput>
Many thanks, Paul
Going to take your word and assume cannot do it with CF because as @Henry pointed out it's SOAP 1.2. So looks like this requires using Java directly, specifically Soap with Attachments API for Java (SAAJ). This is in the java package javax.xml.soap
, which is not part of a standard Java distribution. Instead it's a separate download from Oracle.
Best end to end tutorial I could find on setting up a connection to an endpoint, building & sending a request, and receiving and parsing a response is from IBM's developerworks site. Be forewarned it's involved and doing so will require installing several jars in the ColdFusion installation's classpath from the aforementioned download.
The easiest way is to create Java stubs for your webservice then creating a java object and making calls to java object in coldfusion.
- Create the stubs, jar them, put them in your lib folder and restart coldfusion
- Make sure you can see jar file in your path (look in the cf admin under summary).
use it like this:
<cfobject name="myObj" type="Java" class="your.class.name" action="create">
<cfscript>
args = structNew()
myObj.webservicemethod(args);
</cfscript>
精彩评论