首页 > 编程 > .NET > 正文

Flex与asp.net的WebService通讯

2024-07-10 13:08:16
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  •   本篇随笔主要是关于flex与asp.net的webservice通讯,其中利用了soapheader。(如果你对soapheader不了解,可以参考webservice开发(一) 如何使用soap头)。由此可见webservice在跨平台方面的优势。

      asp.net的webservice代码

    using system;
    using system.web;
    using system.web.services;
    using system.web.services.protocols;

    [webservice(namespace = "http://davidfan.cnblogs.com")]//名称空间应该和flex中定义相同
    [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
    public class service : system.web.services.webservice
    {
        public validatesoapheader header;
        [webmethod]
        [soapheader("header")]
        public string helloworld()
        {
            if (header == null)
            {
                return "请提供验证信息.";
            }
            else
            {
                if (header.name == "admin" && header.password == "admin")
                {
                    if (header.exdate < datetime.now)
                    {
                        return "帐号过期";
                    }
                    else
                    {
                        return "验证成功";
                    }
                }
                else
                {
                    return "用户名或密码错误";
                }
            }
        }

    }

    /**//// <summary>
    /// 继承自soapheader的自定义类
    /// </summary>
    public class validatesoapheader : system.web.services.protocols.soapheader
    {
        public validatesoapheader()
        {
        }
        public validatesoapheader(string name, string password, datetime dt)
        {
            this.name = name;
            this.password = password;
            this.exdate = dt;
        }

        private string name;
        public string name
        {
            get { return name; }
            set { name = value; }
        }

        private string password;
        public string password
        {
            get { return password; }
            set { password = value; }
        }

        private datetime exdate;
        public datetime exdate
        {
            get { return exdate; }
            set { exdate = value; }
        }
    }

      flex的mxml、as代码

      mxml

    <?xml version="1.0" encoding="utf-8"?>
    <mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontfamily="simsun" fontsize="12">
    <mx:script>
    <![cdata[
        import webserviceutility.info;
        import mx.rpc.events.faultevent;
        import mx.rpc.events.resultevent;
        import mx.rpc.soap.soapheader;
       
        private function sendrequest():void
        {
            var content:info = new info();
            content.name = username.text;
            content.password = password.text;
            content.exdate = new date(2010, 3, 10);
            //关键在qname的构造函数
            //第一个参数要和.net的webservice的名称空间相同
            //第二个参数要和.net的webservice的继承自soapheader的自定义类名称相同
            var qname:qname = new qname("http://davidfan.cnblogs.com","validatesoapheader");
            var header:soapheader = new soapheader(qname,content);
           
            ws.clearheaders();
            ws.addheader(header);
            ws.helloworld();
        }
        private function onresult(event:resultevent):void{
                result.text= event.result.tostring();
        }
        private function onfault(event:faultevent):void{
                result.text= event.message.tostring();
        }
    ]]>
    </mx:script>
    <mx:webservice id="ws" wsdl="http://localhost:9200/website2/service.asmx?wsdl"
    result="onresult(event)" fault="onfault(event)"/>
    <mx:textinput id="username" x="98" y="94"/>
    <mx:textinput id="password" x="98" y="124"/>
    <mx:label x="29" y="98" text="用户名:"/>
    <mx:label x="29" y="128" text="密 码:"/>
    <mx:button x="98" y="184" label="调用" click="sendrequest()"/>
    <mx:label x="29" y="156" text="结 果:"/>
    <mx:textinput id="result" x="98" y="154"/>
    </mx:application>

      as

    package webserviceutility
    {
            public class info{
                //该类名称可以和.net中的validatesoapheader不同
                //但字段要对应拼写相同
                public var name:string;
                public var password:string;
                public var exdate:date;
            }
    }
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表