Flex and ASP.net WebService communication

Source: Internet
Author: User
Tags date datetime header soap tostring wsdl
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;
       }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.