日期:2011-02-07  浏览次数:20413 次

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