如何在coldfusion中使用经过身份验证的Web服务?

172*_*729 2 coldfusion wsdl web-services

我正在使用网络服务(由于保密,我无法透露姓名).在获取wsdl页面之前,Web服务要求我使用用户名和密码登录.例如,如果我在Web服务器中输入服务URL,则会提示我输入登录信息.

所以,我假设需要首先验证自己,然后使用wsdl.

但是,我写了一些测试代码,这些代码消耗了w3schools Celsius到Fahrenheit服务,它不需要身份验证并且工作正常.

<!--- 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>
Run Code Online (Sandbox Code Playgroud)

无论如何,我可以调整这个以请求经过身份验证的Web服务吗?请注意这个问题可能有些模糊.但是,如果您需要更多信息,请发表评论以告诉我.

编辑:当我调整我的网络服务的代码,它说我有一个握手失败.

编辑:添加了wsdl文件(更改了命名空间):[dump] http://dumptext.com/vy7Fj2da

小智 5

您可能会遇到握手失败的原因有几个:

  1. 如果您使用的是ColdFusion 9或更早版本,则需要发送SSLv2客户端Hello以进行握手.很可能是因为新的PCI规则,另一方关闭了,这就是你失败的原因.您可以选择解决此问题:

    • 将ColdFusion升级到版本10/11
    • 将当前ColdFusion的Java版本升级到1.7
    • 使用以下不使用underling Java类的自定义标记来建立连接:cfx_http5(http://adiabata.com/cfx_http5.cfm)
  2. 另一方有一个SSL证书,您需要在ColdFusion存储中安装该证书才能成功建立连接.像这样的东西:

    • keytool -importcert -v -alias [别名] -file [证书文件的位置] -keystore D:\ ColdFusion9\runtime\jre\lib\security\cacerts -storepass changeit