使用Flex HttpService 和 ASP.net 通過get,post互動文本或XML格式資料

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.