Flex HttpService,WebService简单介绍

HttpService定义:
 在 MXML 文件中使用 <mx:HTTPService> 标签代表 HTTPService 对象。当调用 HTTPService 对象的 send() 方法时,
 将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选择向指定 URL 传递参数。如果没有使用基于服务器的代理服务,
 则只能使用 HTTP GET 或 POST 方法。如果将 useProxy 属性设置为 true 并使用基于服务器的代理服务,则还可以使用
 HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。
 
 MXML 语法如下: 
 <mx:HTTPService
         concurrency="multiple|single|last"
         contentType="application/x-www-form-urlencoded|application/xml"
     destination="DefaultHTTP"
     id="No default."
     method="GET|POST|HEAD|OPTIONS|PUT|TRACE|DELETE"
     resultFormat="object|array|xml|e4x|flashvars|text"
     showBusyCursor="false|true"
    makeObjectsBindable="false|true"
    url="No default."
    useProxy="false|true"
    xmlEncode="No default."
    xmlDecode="No default."
    fault="No default."
    result="No default."
 />
 常用属性:
        id
    method
    resultFormat
    url
    useProxy
 常用事件:
    fault
      result
数据加载请求例子

 客户端:

通过标签来实现

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
    creationComplete="application1_creationCompleteHandler(event)">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   import mx.rpc.events.FaultEvent;
   import mx.rpc.events.ResultEvent;
   /**
    * 调用失败处理
    */
   protected function http_faultHandler(event:FaultEvent):void
   {
     Alert.show(event.fault.toString());
   }

   /**
    * 返回结果处理
    */
   protected function http_resultHandler(event:ResultEvent):void
   {
     Alert.show(event.result.toString());
   }

   /**
    * 初始化
    */
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
      http.send();
   }

  ]]>
 </mx:Script>
 
 <mx:HTTPService id="http" resultFormat="array" method="POST" url="http://localhost:6196/Web/Default.aspx"
     useProxy="false" fault="http_faultHandler(event)" result="http_resultHandler(event)"/>
</mx:Application>

通过代码来实现

 

   /**
    * 调用失败处理
    */
   protected function http_faultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.toString());
   }

   /**
    * 返回结果处理
    */
   protected function http_resultHandler(event:ResultEvent):void
   {
    Alert.show(event.result.toString());
   }

   /**
    * 初始化
    */
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
    var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
    service.url = "http://localhost:6196/Web/Default.aspx";
    service.useProxy = false;
    service.resultFormat="text";
    service.addEventListener(ResultEvent.RESULT,http_resultHandler);
    service.addEventListener(FaultEvent.FAULT,http_resultHandler);
    service.send();
   }

 

服务端:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Hello Flex HttpService");
    }

 参数传递方式共两种

客户端

第一种

/**
    * 调用失败处理
    */
   protected function http_faultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.toString());
   }

   /**
    * 返回结果处理
    */
   protected function http_resultHandler(event:ResultEvent):void
   {
    Alert.show(event.result.toString());
   }

   /**
    * 初始化
    */
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
    var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
    service.url = "http://localhost:6196/Web/Default.aspx?a=yang&b=xiao";
    service.useProxy = false;
    service.resultFormat="text";
    service.addEventListener(ResultEvent.RESULT,http_resultHandler);
    service.addEventListener(FaultEvent.FAULT,http_resultHandler);
    service.send();
   }

第二种

/**
    * 调用失败处理
    */
   protected function http_faultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.toString());
   }

   /**
    * 返回结果处理
    */
   protected function http_resultHandler(event:ResultEvent):void
   {
    Alert.show(event.result.toString());
   }

   /**
    * 初始化
    */
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
    var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
    service.url = "http://localhost:6196/Web/Default.aspx";
    service.useProxy = false;
    service.resultFormat="text";
    service.addEventListener(ResultEvent.RESULT,http_resultHandler);
    service.addEventListener(FaultEvent.FAULT,http_resultHandler);
    var param:URLVariables = new URLVariables();
    param.a = "yang";
    param.b = "xiao";
    service.send(param);
   }

服务端

 public string A
    {
        get
        {
            object o = Request.QueryString["a"];
            if (o == null)
            {
                return "";
            }
            return o.ToString();
        }
    }
    public string B
    {
        get
        {
            object o = Request.QueryString["b"];
            if (o == null)
            {
                return "";
            }
            return o.ToString();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Hello Flex HttpService\r\n");
        Response.Write(A + "\r\n");
        Response.Write(B + "\r\n");
    }

 返回的数据类型

转载于:https://www.cnblogs.com/yangxiao24/archive/2010/11/02/1867574.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值