本篇随笔主要是关于Flex与asp.net的WebService通讯,其中利用了SoapHeader

ASP.NET的WebService代码

  1using System;
  2
  3using System.Web;
  4
  5using System.Web.Services;
  6
  7using System.Web.Services.Protocols;
  8
  9[WebService(Namespace = "http://davidfan.cnblogs.com")]//名称空间应该和Flex中定义相同
 10
 11[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 12
 13public class Service : System.Web.Services.WebService
 14
 15{
 16
 17  public ValidateSoapHeader header;
 18
 19  [WebMethod]
 20
 21  [SoapHeader("header")]
 22
 23  public string HelloWorld()
 24
 25  {
 26
 27    if (header == null)
 28
 29    {
 30
 31      return "请提供验证信息.";
 32
 33    }

 34
 35    else
 36
 37    {
 38
 39      if (header.Name == "admin" && header.Password == "admin")
 40
 41      {
 42
 43        if (header.ExDate < DateTime.Now)
 44
 45        {
 46
 47          return "帐号过期";
 48
 49        }

 50
 51        else
 52
 53        {
 54
 55          return "验证成功";
 56
 57        }

 58
 59      }

 60
 61      else
 62
 63      {
 64
 65        return "用户名或密码错误";
 66
 67      }

 68
 69    }

 70
 71  }

 72
 73}

 74
 75/**//// <summary>
 76
 77/// 继承自SoapHeader的自定义类
 78
 79/// </summary>

 80
 81public class ValidateSoapHeader : System.Web.Services.Protocols.SoapHeader
 82
 83{
 84
 85  public ValidateSoapHeader()
 86
 87  {
 88
 89  }

 90
 91  public ValidateSoapHeader(string name, string password, DateTime dt)
 92
 93  {
 94
 95    this.name = name;
 96
 97    this.password = password;
 98
 99    this.exDate = dt;
100
101  }

102
103  private string name;
104
105  public string Name
106
107  {
108
109    get return name; }
110
111    set { name = value; }
112
113  }

114
115  private string password;
116
117  public string Password
118
119  {
120
121    get return password; }
122
123    set { password = value; }
124
125  }

126
127  private DateTime exDate;
128
129  public DateTime ExDate
130
131  {
132
133    get return exDate; }
134
135    set { exDate = value; }
136
137  }

138
139}

140
Flex的mxml、as代码
mxml:
 1<?xml version="1.0" encoding="utf-8"?>
 2
 3<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontFamily="simsun" fontSize="12">
 4
 5
 6<mx:Script>
 7
 8<![CDATA[
 9
10  import WebServiceUtility.Info;
11
12  import mx.rpc.events.FaultEvent;
13
14  import mx.rpc.events.ResultEvent;
15
16  import mx.rpc.soap.SOAPHeader;
17
18  
19
20  private function SendRequest():void
21
22  {
23
24    var content:Info = new Info();
25
26    content.Name = UserName.text;
27
28    content.Password = PassWord.text;
29
30    content.ExDate = new Date(2010, 3, 10);
31
32    //关键在QName的构造函数
33
34    //第一个参数要和.Net的WebService的名称空间相同
35
36    //第二个参数要和.Net的WebService的继承自SoapHeader的自定义类名称相同
37
38    var qname:QName = new QName("http://davidfan.cnblogs.com","ValidateSoapHeader");
39
40    var header:SOAPHeader = new SOAPHeader(qname,content);
41
42    
43
44    ws.clearHeaders();
45
46    ws.addHeader(header);
47
48    ws.HelloWorld();
49
50  }
51
52  private function OnResult(event:ResultEvent):void{
53
54      Result.text= event.result.toString();
55
56  }
57
58  private function OnFault(event:FaultEvent):void{
59
60      Result.text= event.message.toString();
61
62  }
63
64]]>
65
66</mx:Script>
67
68<mx:WebService id="ws" wsdl="http://localhost:9200/WebSite2/Service.asmx?WSDL"
69
70result="OnResult(event)" fault="OnFault(event)"/>
71
72<mx:TextInput id="UserName" x="98" y="94"/>
73
74<mx:TextInput id="PassWord" x="98" y="124"/>
75
76<mx:Label x="29" y="98" text="用户名:"/>
77
78<mx:Label x="29" y="128" text="密 码:"/>
79
80<mx:Button x="98" y="184" label="调用" click="SendRequest()"/>
81
82<mx:Label x="29" y="156" text="结 果:"/>
83
84<mx:TextInput id="Result" x="98" y="154"/>
85
86</mx:Application>

as:
 1package WebServiceUtility
 2
 3{
 4
 5    public class Info{
 6
 7      //该类名称可以和.Net中的ValidateSoapHeader不同
 8
 9      //但字段要对应拼写相同
10
11      public var Name:String;
12
13      public var Password:String;
14
15      public var ExDate:Date;
16
17    }

18
19}

20
posted on 2008-09-22 14:35  schyu  阅读(304)  评论(0编辑  收藏  举报