1729
1729

Reputation: 175

How to consume an authenticated webservice in coldfusion?

I am consuming a web service (I cannot reveal the name due to being confidential). The web service requires me to login w/ username and password before I get the wsdl page. For example, if I enter the service url in a web server, it prompts me to enter the login information.

So, I am assuming need to first authenticate myself, then use the wsdl.

However, I wrote some test code that consumes the w3schools Celsius to Fahrenheit service, which DOES NOT require authentication and works perfectly.

<!--- soap request in xml --->
<cfsavecontent variable="soap">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    xmlns:tns="http://www.w3schools.com/webservices/" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:s="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <SOAP-ENV:Body>
        <tns:CelsiusToFahrenheit xmlns:tns="http://www.w3schools.com/webservices/">
            <tns:Celsius>34</tns:Celsius>
        </tns:CelsiusToFahrenheit>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</cfsavecontent>

<!---Sending a post request accessing the defined SOAPAction CelsiusToFahrenheit--->
<cfhttp url="http://www.w3schools.com/webservices/tempconvert.asmx" method="post" result="httpresponse">
    <cfhttpparam type="header" name="content-type" value="text/xml"> 
    <cfhttpparam type="header" name="SOAPAction" value="""http://www.w3schools.com/webservices/CelsiusToFahrenheit"""> 
    <cfhttpparam type="header" name="content-length" value="#len(soap)#"> 
    <cfhttpparam type="header" name="charset" value="utf-8"> 
    <cfhttpparam type="xml" name="message" value="#trim(soap)#">  
</cfhttp>

<!---Output the result if http status is 200 (we can do error handling also) --->
<cfif find( "200", httpresponse.statusCode )>
    <cfset soapResponse = xmlParse(httpresponse.fileContent) />

    <cfset responseNodes = xmlSearch(soapResponse, "//*[ local-name() = 'CelsiusToFahrenheitResult' ]") />

    <cfoutput>Fahrenheit: #responseNodes[ 1 ].xmlText#</cfoutput>
</cfif>

Is there anyway I could adapt this to ask for the authenticated web service? Please note this question may be somewhat vague. However, please comment to let me know if you need more information.

EDIT: When I adapted the code for my web service, it says I have a hand-shake failure.

EDIT: added the wsdl file (changed the namespace): [dump] http://dumptext.com/vy7Fj2da

Upvotes: 2

Views: 1117

Answers (1)

Rado
Rado

Reputation: 346

There are a few reasons why you might be getting a hand-shake failure:

  1. If you are using ColdFusion 9 or earlier version you are sending a SSLv2 Client Hello for a hand-shake. Most likely because of new PCI regulation the other side has that turned off and that is why you are getting a failure. Your options to fix this are:

    • Upgrade ColdFusion to version 10/11
    • Upgrade the Java version of your current ColdFusion to 1.7
    • Use the following custom tag that doesn't use underling Java classes to make the connection: cfx_http5 (http://adiabata.com/cfx_http5.cfm)
  2. The other side has a SSL certificate that you need to install in your ColdFusion store to make a successful connection. Something like this:

    • keytool -importcert -v -alias [alias name] -file [location of certificate file] -keystore D:\ColdFusion9\runtime\jre\lib\security\cacerts -storepass changeit

Upvotes: 5

Related Questions