This article focuses on the communication between Flex and asp.net WebService, and uses SoapHeader. (If you do not know about SoapHeader, refer to WebService development (1) how to use the Soap header ). This shows the advantages of WebService in cross-platform.
WebService code of ASP. NET
The following is a reference clip:
Using System;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
[WebService (Namespace = "http://davidfan.cnblogs.com")] // The Namespace should be the same as the one defined in 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 "Please provide verification information .";
}
Else
{
If (header. Name = "admin" & header. Password = "admin ")
{
If (header. ExDate <DateTime. Now)
{
Return "account expired ";
}
Else
{
Return "Verification Successful ";
}
}
Else
{
Return "Incorrect username or password ";
}
}
}
}
/** // <Summary>
/// Inherit the custom class from 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 and as code
Mxml
The following is a reference clip:
<? 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 );
// The key is the QName constructor.
// The first parameter must be the same as the. Net WebService namespace.
// The second parameter must be the same as the custom class name inherited from SoapHeader of. Net WebService.
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 = "username:"/>
<Mx: Label x = "29" Y = "128" text = "Password:"/>
<Mx: button x = "98" Y = "184" label = "call" Click = "sendrequest ()"/>
<Mx: Label x = "29" Y = "156" text = "Result:"/>
<Mx: textinput id = "result" x = "98" Y = "154"/>
</MX: Application>
As
The following is a reference clip:
Package webserviceutility
{
Public class Info {
// The class name can be different from ValidateSoapHeader in. Net.
// The spelling of the field must be the same.
Public var Name: String;
Public var Password: String;
Public var ExDate: Date;
}
}