(1) 通过 get交互文本数据,服务器端asp.net为html格式,resultFormat为text格式
flex code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundColor="#FFFFFF"
backgroundAlpha="0">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
public function handlePlain(event:ResultEvent):void
{
shippingOptions.text = event.result.toString();
}
public function handleFault(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</mx:Script>
<mx:HTTPService id="plainRpc"
url="http://aspexamples.adobe.com/flex3/exchangingdata/PlainHttpService.aspx"
result="handlePlain(event)"
fault="handleFault(event)"
resultFormat="text" method="get">
<mx:request>
<zipcode>{zipcode.text}</zipcode>
<pounds>{weight_lb.text}</pounds>
</mx:request>
</mx:HTTPService>
<mx:Label x="56" y="32"
text="Zip Code"
width="55" height="18"
textAlign="right"
fontWeight="bold" />
<mx:Label x="56" y="58"
text="Weight"
width="55" height="18"
textAlign="right"
fontWeight="bold" />
<mx:TextInput id="zipcode"
x="130" y="32"
width="160" height="22" />
<mx:TextInput id="weight_lb"
x="130" y="58"
width="160" height="22" />
<mx:Button x="130" y="95"
label="Get Shipping Options"
click="plainRpc.send()"
width="160" height="22" />
<mx:Text id="shippingOptions"
x="56" y="150"
width="310" height="133"
fontWeight="bold" />
</mx:Application>
C# code:
<%@ Import Namespace="quickstart" %>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs E)
{
int zipcode;
double weight;
if(Request.RequestType == "POST")
{
zipcode = Int32.Parse(Request.Form["zipcode"].ToString());
weight = Double.Parse(Request.Form["pounds"].ToString());
}
else
{
zipcode = Int32.Parse(Request.QueryString["zipcode"].ToString());
weight = Double.Parse(Request.QueryString["pounds"].ToString());
}
ShippingCalculator shippingcalculator = new ShippingCalculator();
ShippingOption shippingOption = new ShippingOption();
ArrayList al = shippingcalculator.getShippingOptions(zipcode, weight);
StringBuilder stringBuilder;;
foreach(Object obj in al)
{
stringBuilder = new StringBuilder();
shippingOption = (ShippingOption)obj;
stringBuilder.Append(shippingOption.getService());
stringBuilder.Append(": ");
stringBuilder.Append(shippingOption.getPrice());
stringBuilder.Append(" USD" + "/n");
Response.Write(stringBuilder.ToString());
}
}
</script>
(2) 通过 post 交互XML数据,服务器端asp.net为XML格式,resultFormat为e4x格式,
flex code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundColor="#FFFFFF"
backgroundAlpha="1">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
public function handleXml(event:ResultEvent):void
{
shippingOptionsGrid.dataProvider = event.result.option;
}
public function handleFault(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</mx:Script>
<mx:HTTPService id="xmlRpc"
url="http://aspexamples.adobe.com/flex3/exchangingdata/xmlHttpService.aspx"
result="handleXml(event)"
fault="handleFault(event)"
resultFormat="e4x" method="post">
<mx:request>
<zipcode>{zipcode.text}</zipcode>
<pounds>{weight_lb.text}</pounds>
</mx:request>
</mx:HTTPService>
<mx:Label x="56" y="32"
text="Zip Code"
width="55" height="18"
textAlign="right"
fontWeight="bold" />
<mx:Label x="56" y="58"
text="Weight"
width="55" height="18"
textAlign="right"
fontWeight="bold" />
<mx:TextInput id="zipcode"
x="130" y="32"
width="160" height="22" />
<mx:TextInput id="weight_lb"
x="130" y="58"
width="160" height="22" />
<mx:Button x="130" y="95"
label="Get Shipping Options"
click="xmlRpc.send()"
width="160" height="22" />
<mx:DataGrid id="shippingOptionsGrid"
x="80" y="141"
width="262" height="92"
editable="false"
enabled="true">
<mx:columns>
<mx:DataGridColumn headerText="Service" dataField="service" />
<mx:DataGridColumn headerText="Price" dataField="price" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
asp.net code:
<%@ Import Namespace="quickstart" %>
<script language="C#" runat="server" ContentType="text/xml" >
public string str="";
public void Page_Load(Object sender, EventArgs E)
{
int zipcode;
double weight;
if(Request.RequestType == "POST")
{
zipcode = Int32.Parse(Request.Form["zipcode"].ToString());
weight = Double.Parse(Request.Form["pounds"].ToString());
}
else
{
zipcode = Int32.Parse(Request.QueryString["zipcode"].ToString());
weight = Double.Parse(Request.QueryString["pounds"].ToString());
}
ShippingCalculator shippingcalculator = new ShippingCalculator();
ShippingOption shippingOption = new ShippingOption();
ArrayList al = shippingcalculator.getShippingOptions(zipcode, weight);
StringBuilder stringBuilder = new StringBuilder("<options>");
foreach(Object obj in al)
{
shippingOption = (ShippingOption)obj;
stringBuilder.Append("<option><service>");
stringBuilder.Append(shippingOption.getService());
stringBuilder.Append("</service><price>");
stringBuilder.Append(shippingOption.getPrice());
stringBuilder.Append("</price></option>" );
}
stringBuilder.Append("</options>");
str = stringBuilder.ToString();
}
</script>
<?xml version="1.0" encoding="utf-8"?>
<%
Response.ContentEncoding = Encoding.UTF8;
Response.Write(str);
%>