北大青鸟中博
在线报名 | 成绩查询 | 中博南京校区 | 中博技术论坛 | 邮箱:注册@zbaccp.com 登录
高中生就业方案 大学生就业方案 在职、转行、待业人员就业方案 中博校区环境 2008高考特招 北大青鸟招生公告 校庆6周年活动
您的位置:新闻动态 - 行业新闻

Flex与asp.net的WebService通讯
作者:中博 出处:中博 时间:2008-02-29

本篇随笔主要是关于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;
        }
徐州北大青鸟   北大青鸟徐州中博  徐州IT培训 徐州计算机培训  徐州电脑学校


>>更多新闻

前一篇:Spring的优秀工具类盘点(一)文件资源操作和Web 相关工具类
后一篇:.NET环境下几种不同的邮件发送解决方案

 
在线咨询  
黄老师 陆老师
韩老师 值班老师
中心简介
校区简介
校区环境
关于北大青鸟
特色优势
常见问题解答
 
 
ACCP软件工程师课程
什么是ACCP
ACCP5.0课程介绍
各课程定位与特色
各阶段课程
教学模式
ACCP5.0课程体系
ACCP认证证书
 
 
全国免费服务电话:800-828-5191(电信用户使用)
  报名咨询电话:0516 - 85628888 85629999 黄老师 陆老师
地址:徐州市解放南路181科技培训中心(六中对面)
乘车路线:11、19、20、65、39、60、51、64、609、游1、游2至和平路六中站下 苏ICP备08102142号