Flex与asp.net通过HttpService通信示例
2010-12-24 00:08
441 查看
1.HttpService,实际上就是传统的网站,通过get和post的方法传递数据或网页环境变量传递数据。
2.flex的HttpService组件,封装了了处理httpservice的一系列处理方法事件等。
示例(与web页交互)
1.asp.net web代码(VS2008)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private String conStr=System.Configuration.ConfigurationManager.ConnectionStrings["FlexdemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
/*
if (Request.QueryString["username"].Equals("chu888"))
{
Response.Write("<Result>");
Response.Write("<chu888>ok</chu888>");
Response.Write("</Result>");
}
else
{
Response.Write("<Result>");
Response.Write("<chu888>error</chu888>");
Response.Write("</Result>");
} */
if (Request["call"] != null)
{
switch (Request["call"])
{
case "GetDataById":
GetDataById(int.Parse(Request["id"]));
break;
case "GetAll":
GetAll();
break;
case "SaveData":
SaveData(Request["name"], Request["email"]);
break;
case "AnotherMethod":
// your method call
break;
}
}
}
private void GetAll()
{
SqlDataAdapter da = new SqlDataAdapter("SELECT id, name, email FROM test", conStr);
// Bad code. Possible SQL injection. Just for demo purpose
DataSet ds = new DataSet();
da.Fill(ds);
ds.DataSetName = "parent";
ds.Tables[0].TableName = "child";
// Output data in XML format
Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
Response.Write(ds.GetXml());
/*********输出格式*******************
<?xml version="1.0" encoding="utf-8"?>
<parent>
<child>
<id>2</id>
<name>Praveen.V.Nair</name>
<email>ninethsense@hotmail.com</email>
</child>
</parent>
****************************/
}
private void GetDataById(int id)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT id, name, email FROM test WHERE id=" + id.ToString(), conStr);
// Bad code. Possible SQL injection. Just for demo purpose
DataSet ds = new DataSet();
da.Fill(ds);
ds.DataSetName = "parent";
ds.Tables[0].TableName = "child";
// Output data in XML format
Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
Response.Write(ds.GetXml());
}
private void SaveData(string name, string email)
{
string cmdtext = "INSERT INTO test (name, email) VALUES ('" + name + "','" + email + "');";
// Bad code. Possible SQL injection. Just for demo purpose
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdtext, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
2.flex代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="horizontal"
backgroundColor="#FFFFFF"
creationComplete="init()" >
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private function init():void{
// Initialize if you have something
}
private function httpFaultHandler(event:FaultEvent):void{
// If you want to something
Alert.show("There was a problem","Error");
}
private function httpResultHandler(event:ResultEvent):void{
// If you want to something
}
private function PopulateDataGridById(event:MouseEvent):void {
var params:Object = {};
params["call"] = "GetDataById";
params["id"] = txtID.text;
hsData.send(params);
}
private function PopulateDataGridAll(event:MouseEvent):void {
Alert.show("demo");
var params:Object = {};
params["call"] = "GetAll";
hsData.send(params);
}
private function SaveData(event:MouseEvent):void {
// Send data to server
var params:Object = {};
params["call"] = "SaveData";
params["name"] = txtName.text;
params["email"] = txtEmail.text;
hsData.send(params);
// reload datagrid
params = new Object();
params["call"] = "GetAll";
hsData.send(params);
}
]]>
</mx:Script>
<mx:HTTPService id="hsData" url="http://localhost/WebHost/Default.aspx"
fault="httpFaultHandler(event)"
result="httpResultHandler(event)"
method="POST"
/>
<mx:DataGrid id="dg" dataProvider="{hsData.lastResult.parent.child}">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="id"/>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="EMail" dataField="email"/>
</mx:columns>
</mx:DataGrid>
<mx:Form >
<mx:HBox>
<mx:Panel width="250" height="86" layout="absolute" title="Get all records">
<mx:FormItem x="35.5" y="10">
<mx:Button label="Get method: GetAll()" click="PopulateDataGridAll(event)"/>
</mx:FormItem>
</mx:Panel>
<mx:Panel width="250" height="92" layout="absolute" title="Get record by id">
<mx:FormItem label="Label" x="10" y="0">
<mx:TextInput id="txtID"/>
</mx:FormItem>
<mx:FormItem x="10" y="28">
<mx:Button label="Get method: GetDataById()" click="PopulateDataGridById(event)"/>
</mx:FormItem>
</mx:Panel>
<mx:Panel width="250" height="145" layout="absolute" title="Put a new record">
<mx:FormItem label="Name" x="11" y="10">
<mx:TextInput id="txtName" />
</mx:FormItem>
<mx:FormItem label="Email" x="10" y="40">
<mx:TextInput id="txtEmail"/>
</mx:FormItem>
<mx:FormItem x="10" y="70">
<mx:Button label="Put method: SaveData()" click="SaveData(event)"/>
</mx:FormItem>
</mx:Panel>
</mx:HBox>
</mx:Form>
</mx:Application>
2.flex的HttpService组件,封装了了处理httpservice的一系列处理方法事件等。
示例(与web页交互)
1.asp.net web代码(VS2008)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private String conStr=System.Configuration.ConfigurationManager.ConnectionStrings["FlexdemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
/*
if (Request.QueryString["username"].Equals("chu888"))
{
Response.Write("<Result>");
Response.Write("<chu888>ok</chu888>");
Response.Write("</Result>");
}
else
{
Response.Write("<Result>");
Response.Write("<chu888>error</chu888>");
Response.Write("</Result>");
} */
if (Request["call"] != null)
{
switch (Request["call"])
{
case "GetDataById":
GetDataById(int.Parse(Request["id"]));
break;
case "GetAll":
GetAll();
break;
case "SaveData":
SaveData(Request["name"], Request["email"]);
break;
case "AnotherMethod":
// your method call
break;
}
}
}
private void GetAll()
{
SqlDataAdapter da = new SqlDataAdapter("SELECT id, name, email FROM test", conStr);
// Bad code. Possible SQL injection. Just for demo purpose
DataSet ds = new DataSet();
da.Fill(ds);
ds.DataSetName = "parent";
ds.Tables[0].TableName = "child";
// Output data in XML format
Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
Response.Write(ds.GetXml());
/*********输出格式*******************
<?xml version="1.0" encoding="utf-8"?>
<parent>
<child>
<id>2</id>
<name>Praveen.V.Nair</name>
<email>ninethsense@hotmail.com</email>
</child>
</parent>
****************************/
}
private void GetDataById(int id)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT id, name, email FROM test WHERE id=" + id.ToString(), conStr);
// Bad code. Possible SQL injection. Just for demo purpose
DataSet ds = new DataSet();
da.Fill(ds);
ds.DataSetName = "parent";
ds.Tables[0].TableName = "child";
// Output data in XML format
Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
Response.Write(ds.GetXml());
}
private void SaveData(string name, string email)
{
string cmdtext = "INSERT INTO test (name, email) VALUES ('" + name + "','" + email + "');";
// Bad code. Possible SQL injection. Just for demo purpose
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdtext, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
2.flex代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="horizontal"
backgroundColor="#FFFFFF"
creationComplete="init()" >
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private function init():void{
// Initialize if you have something
}
private function httpFaultHandler(event:FaultEvent):void{
// If you want to something
Alert.show("There was a problem","Error");
}
private function httpResultHandler(event:ResultEvent):void{
// If you want to something
}
private function PopulateDataGridById(event:MouseEvent):void {
var params:Object = {};
params["call"] = "GetDataById";
params["id"] = txtID.text;
hsData.send(params);
}
private function PopulateDataGridAll(event:MouseEvent):void {
Alert.show("demo");
var params:Object = {};
params["call"] = "GetAll";
hsData.send(params);
}
private function SaveData(event:MouseEvent):void {
// Send data to server
var params:Object = {};
params["call"] = "SaveData";
params["name"] = txtName.text;
params["email"] = txtEmail.text;
hsData.send(params);
// reload datagrid
params = new Object();
params["call"] = "GetAll";
hsData.send(params);
}
]]>
</mx:Script>
<mx:HTTPService id="hsData" url="http://localhost/WebHost/Default.aspx"
fault="httpFaultHandler(event)"
result="httpResultHandler(event)"
method="POST"
/>
<mx:DataGrid id="dg" dataProvider="{hsData.lastResult.parent.child}">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="id"/>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="EMail" dataField="email"/>
</mx:columns>
</mx:DataGrid>
<mx:Form >
<mx:HBox>
<mx:Panel width="250" height="86" layout="absolute" title="Get all records">
<mx:FormItem x="35.5" y="10">
<mx:Button label="Get method: GetAll()" click="PopulateDataGridAll(event)"/>
</mx:FormItem>
</mx:Panel>
<mx:Panel width="250" height="92" layout="absolute" title="Get record by id">
<mx:FormItem label="Label" x="10" y="0">
<mx:TextInput id="txtID"/>
</mx:FormItem>
<mx:FormItem x="10" y="28">
<mx:Button label="Get method: GetDataById()" click="PopulateDataGridById(event)"/>
</mx:FormItem>
</mx:Panel>
<mx:Panel width="250" height="145" layout="absolute" title="Put a new record">
<mx:FormItem label="Name" x="11" y="10">
<mx:TextInput id="txtName" />
</mx:FormItem>
<mx:FormItem label="Email" x="10" y="40">
<mx:TextInput id="txtEmail"/>
</mx:FormItem>
<mx:FormItem x="10" y="70">
<mx:Button label="Put method: SaveData()" click="SaveData(event)"/>
</mx:FormItem>
</mx:Panel>
</mx:HBox>
</mx:Form>
</mx:Application>
相关文章推荐
- 使用FLEX的HttpService与ASP.NET进行动态交互
- flex 通过HTTPService与php通信
- 使用FLEX的HttpService与ASP.NET进行动态交互之一(转载)
- Flex4:利用HttpService与ASP.NET传输JSON数据(登录为例)
- Flex连接数据库(httpservice、asp.net、XML)
- 使用FLEX的HttpService与ASP.NET进行动态交互之一
- 使用FLEX的HttpService与ASP.NET进行动态交互之一
- java Web与Flex通过HTTP service 通信 并解析Map 数据。
- asp.net向网页输出文件及flex发送HttpService的总结(个人笔记)
- 使用FLEX的HttpService与ASP.NET进行动态交互之一
- flex通过HTTPService与java通信例子
- 【转】Flex4:利用HttpService与ASP.NET传输JSON数据(登录为例)
- ASP.net通过http调用验证码短信接口源码
- [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (四)
- Java与Flex学习笔记(5)----Flex与Java通信之HttpService方式
- Jquery 调用asp.net ajax (web service/static page method)的示例(二)---复杂参数
- ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp