Asp.net|web
This essay is mainly about Flex and ASP.net WebService communication, which utilizes the SoapHeader. (If you don't understand soapheader, you can refer to WebService development (a) How to use the SOAP header). This shows the advantages of webservice in Cross-platform.
Asp. NET's WebService code
Using System;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;
[WebService (Namespace = "http://davidfan.cnblogs.com")]//namespace should be the same as 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 "Validation Successful";
}
}
Else
{
Return "User name or password error";
}
}
}
}
/**////<summary>
Inheriting custom classes 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, as code
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);
Key in the QName constructor
The first argument is to be and. NET has the same namespace as the WebService
The second argument is to be and. NET WebService inherits the same name as the SoapHeader custom class
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= "" y= "" "/>"
<mx:textinput id= "PassWord" x= "y=" 124
<mx:label x= "y=" "text=" User name: "/>
<mx:label x= "y=" 128 "text=" Password: "/>
<mx:button x= "y=" 184 "label=" calls "click=" SendRequest () "/>"
<mx:label x= "y=" 156 "text=" Result: "/>
<mx:textinput id= "Result" x= "y=" "154"/>
</mx:Application>
As
package webserviceutility
{
public class info{
//The class name can be and. NET Validatesoapheader different
//But fields should be spelled the same
public var name:string;
public var password:string;
public var exdate:date;
}
}