Flex与asp.net的WebService通讯

80酷酷网    80kuku.com

  asp.net|web

  本篇随笔主要是关于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;
        }
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: