使用Flex HttpService 和 ASP.net 通过get,post交互文本或XML格式数据

4 篇文章 0 订阅

(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);
%>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值