加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

ColdFusion 编写WebService 示例

发布时间:2020-12-16 21:38:25 所属栏目:安全 来源:网络整理
导读:1.开发 Web Services,编写cfcdemo.cfc组件,代码如下: cfcomponent style? ="document" namespace = "http://www.mycompany.com/" serviceportname = "RestrictedEmpInfo" porttypename = "RestrictedEmpInfo" bindingname = "myns:RestrictedEmpInfo" dis

  1.开发 Web Services,编写cfcdemo.cfc组件,代码如下:  

<cfcomponent style?="document"
namespace = "http://www.mycompany.com/"
serviceportname = "RestrictedEmpInfo"
porttypename = "RestrictedEmpInfo"
bindingname = "myns:RestrictedEmpInfo"
displayname = "RestrictedEmpInfo"
hint = "RestrictedEmpInfo">
  <cffunction name = "getEname" access = "remote" returntype="xml" output="no" >
    <cfargument name = "dname" type = "string" required = "false">
    <cfargument name = "loc" type = "string" required = "false">
    <cfquery name = "dt" datasource = "scott">
      select * from dept where
      <cfif arguments.dname neq "">
        dname = #arguments.dname#
      </cfif>
    </cfquery>
    <cfxml variable="Books">
      <XML>
        <cfoutput query="rs">
          <book>
            <bname>#deptno#</bname>
            <isbn>#dname#</isbn>
            <writer>#loc#</writer>
          </book>
        </cfoutput>
      </XML>
    </cfxml>
    <cfreturn Books>
  </cffunction>
</cfcomponent>

?<!---
??ColdFusion开发 Web Services,只需要在ColdFusion组件(.cfc文件)中,
??把需要作为 Web Services 的method的access类型定义为remote就可以了(access="remote")。
?--->

2.测试一下,访问这个cfc组件,注意URL路径后边要加上 ?wsdl 。

eg:http://localhost/mysys/cfcdemo.cfc?wsdl 

显示效果如下:   

  ?3.开发测试页面,编写webservice.cfm程序调用①开发的 Web service ,代码如下:

   <cfset sWebServiceUrl = "http://localhost/cfcdemo.cfc?wsdl">
    <cfinvoke webservice="#sWebServiceUrl#"

      component = "cfcdemo"

      method = "getdept"

      returnVariable = "dept">
      <cfinvokeargument?? name = "dname" value="10"/>?  

      <!--- 访问参数,与参数值,对应.cfc中的??cfargument?? --->?
      <cfinvokeargument? name = "loc" value=""/>
    </cfinvoke>
   <cfdump var="#dept#">

4.说明:调用 Web Service 时,webservice.cfm部分和cfcdemo.cfc?wsdl对应如下:

  <cffunction></cffunction>? ?

  <!--- cfc组件中的方法,在cfcomponent中可以有多个? --->?

  method?-- 对应cfc组件中的方法,<cffunction>标签内name属性的值
  timeout?-- 设置请求超时秒数
  returnVariable?-- 自定义的变量,里边存放的是 Web Service 返回的值


?<!---传递参数--->? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
?<cfinvokeargument?name="empno" value="7788"/> --

?<!---接收参数--->

<cfargument? name="empno" type="string" required="true">

?<cfoutput>#dept#</cfoutput> -- 输出 Web Service

<!--- 注意返回类型一定要与 returntype 对应--->?

本文日期2018-11-08

文章参考博客anfslove?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读