积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信

简介:
+关注继续查看
索引页]
[源码下载]


积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信


作者:webabcd


介绍
Flash ActionScript 3.0 以文本形式与ASP.NET通信、以XML形式与ASP.NET通信和以JSON形式与ASP.NET通信


示例
Text.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class Text : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                string s = "name: " + Request.QueryString["name"] + "; age: " + Request.QueryString["age"]; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType = "text/plain"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
Xml.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class Xml : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                string s = @"<?xml version=""1.0"" encoding=""utf-8""?> 
InBlock.gif                        <root> 
InBlock.gif                                <person name=""webabcd"" age=""27""> 
InBlock.gif                                        <salary>1000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name=""webabcdefg"" age=""37""> 
InBlock.gif                                        <salary>2000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name=""webabcdefghijklmn"" age=""47""> 
InBlock.gif                                        <salary>3000</salary> 
InBlock.gif                                </person> 
InBlock.gif                        </root>"; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType = "text/xml"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
JSON.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class JSON : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Person person = new Person(); 
InBlock.gif                person.Name = "webabcd"
InBlock.gif                person.Age = 27; 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.ClearContent(); 
InBlock.gif                // HttpContext.Current.Response.ContentType = "application/json"; 
InBlock.gif                HttpContext.Current.Response.ContentType = "text/plain"
InBlock.gif 
InBlock.gif                // 把person对象序列化成JSON 
InBlock.gif                System.Runtime.Serialization.DataContractJsonSerializer dcjs = new System.Runtime.Serialization.DataContractJsonSerializer(person.GetType()); 
InBlock.gif                dcjs.WriteObject(HttpContext.Current.Response.OutputStream, person); 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.End(); 
InBlock.gif        } 
InBlock.gif
InBlock.gif 
/// <summary> 
/// Person类 
/// </summary> 
InBlock.gif[System.Runtime.Serialization.DataContract] 
InBlock.gifpublic class Person 
InBlock.gif
InBlock.gif        private string _name; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 姓名 
InBlock.gif        /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif        public string Name 
InBlock.gif        { 
InBlock.gif                get { return _name; } 
InBlock.gif                set { _name = value; } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private int _age; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 年龄 
InBlock.gif        /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif        public int Age 
InBlock.gif        { 
InBlock.gif                get { return _age; } 
InBlock.gif                set { _age = value; } 
InBlock.gif        } 
InBlock.gif}
 
Net.as
package 

        import flash.display.Sprite; 
        import flash.net.URLLoader; 
        import flash.net.URLRequest; 
        import flash.net.URLVariables; 
        import flash.net.URLRequestMethod; 
        import flash.events.Event; 
         
        // 对JSON的支持 
        import com.adobe.serialization.json.JSON; 
         
        public class Net extends Sprite 
        { 
                public function Net() 
                { 
                        // 以文本形式与ASP.NET通信 
                        showText(); 
                         
                        // 以XML形式与ASP.NET通信 
                        showXml(); 
                         
                        // 以JSON形式与ASP.NET通信 
                        showJSON(); 
                } 
                 
                // 以文本形式与ASP.NET通信 
                function showText():void 
                { 
                        var v:URLVariables = new URLVariables("name=webabcd&age=27"); 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Text.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, textCompleteHandler); 
                } 
                 
                function textCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = URLLoader(event.target); 
                         
                        trace(l.data); 
                        // output: name: webabcd; age: 27 
                } 
                 
                // 以XML形式与ASP.NET通信 
                function showXml():void 
                { 
                        var v:URLVariables = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Xml.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, xmlCompleteHandler); 
                } 
                 
                function xmlCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = event.target as URLLoader; 
                        var xml:XML = new XML(l.data); 
                         
                        for each(var v in xml.person) 
                        { 
                                trace("姓名:" + v.@name + ";年龄:" + v.@age + ";薪水:" + v.salary); 
                        } 
                        // output:    
                        // 姓名:webabcd;年龄:27;薪水:1000 
                        // 姓名:webabcdefg;年龄:37;薪水:2000 
                        // 姓名:webabcdefghijklmn;年龄:47;薪水:30 
                } 
                 
                // 以JSON形式与ASP.NET通信 
                function showJSON():void 
                { 
                        var v:URLVariables = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/JSON.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, jsonCompleteHandler); 
                } 
                 
                function jsonCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = event.target as URLLoader; 
                         
                        var v:* = JSON.decode(l.data); 
                         
                        trace("姓名:" + v.Name + ";年龄:" + v.Age); 
                        // output: 姓名:webabcd;年龄:27 
                } 
        } 
}
 
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342184,如需转载请自行联系原作者

相关文章
|
13天前
|
XML JSON JavaScript
Java xml和json互相转换方法
Java xml和json互相转换方法
31 0
|
2月前
|
XML 数据采集 JSON
scrapy_selenium爬取Ajax、JSON、XML网页:豆瓣电影
在网络爬虫的开发过程中,我们经常会遇到一些动态加载的网页,它们的数据不是直接嵌入在HTML中,而是通过Ajax、JSON、XML等方式异步获取的。这些网页对于传统的scrapy爬虫来说,是很难直接解析的。那么,我们该如何使用scrapy_selenium来爬取这些数据格式的网页呢?本文将为你介绍scrapy_selenium的基本原理和使用方法,并给出一个实际的案例。
|
2月前
|
XML 存储 JSON
文本数据交换格式 -- JSON
文本数据交换格式 -- JSON
28 0
|
3月前
|
XML 存储 JSON
xml与json的区别和跨域
xml与json的区别和跨域
|
5月前
|
JSON 关系型数据库 MySQL
【Python】【MySQL】Python将JSON数据以文本形式存放到MySQL的Text类型字段中
【Python】【MySQL】Python将JSON数据以文本形式存放到MySQL的Text类型字段中
110 0
|
9月前
|
JSON 数据格式
解析json文本
解析json文本
103 0
|
9月前
|
XML SQL JSON
Spring-Batch读取数据 文本数据 数据库数据 XML数据 JSON数据
Spring-Batch读取数据 文本数据 数据库数据 XML数据 JSON数据
Spring-Batch读取数据 文本数据 数据库数据 XML数据 JSON数据
|
9月前
|
XML JSON Java
Spring Batch输出文本数据 XML数据 JSON数据 数据库
Spring Batch输出文本数据 XML数据 JSON数据 数据库
|
10月前
|
存储 SQL JSON
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
198 1
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
|
JSON 数据格式
解析json文本
解析json文本
83 0
相关产品
云迁移中心
推荐文章
更多