在Flash中調用WS時的身分識別驗證_1:Hello

來源:互聯網
上載者:User
因為熟悉的WS的構造方式是基於Asp.net下的,下面所說的都是基於Asp.net的。
    
    Flash作為UI層,有下列好處:
1、 不依賴於具體的平台
2、 輕量級,可以輕易的構造富用戶端程式。
3、 可以給使用者更好的體驗。

但是,Flash在構建複雜的應用時,無法直接與資料庫進行互動,必須依賴於其他的中介層進行,比如FlashRemtoing、PHP等等。各種方式都有優缺點。
在Flash與Asp.net進行互動時,最好的方式就是使用WebService。

下面構造第一個最簡單的例子,從伺服器端開始:
<%@ WebService Language="c#" Class="wsLearn.test" %>
//這句話聲明了這是一個WebService。
using System;
using System.Web;
using System.Web.Services;
//建立WS時必須引用的類。
namespace wsLearn{
//ws “test”所在的命名空間
[WebService(Namespace="http://www.dxlschool.com/ws")]
public class test:System.Web.Services.WebService{
    public test(){
        //do nothing
        }
        [WebMethod]
    public string hello(){
        return "你好!!";
        }

就這樣,一個最簡單的WS構造好了,他只包含一個方法hello,把它放到你的支援asp.net的虛擬目錄下,在瀏覽器中輸入地址可以查看該ws啟動並執行狀況。
比如我在IE中輸入如下地址http://localhost/ws/test.asmx
IE中會給出下列提示:

test
支援下列操作。有關正式定義,請查看服務說明。
Hello

    點擊”hello”,ws會返回一段XML檔案。

  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://www.dxlschool.com/ws">你好!!</string>

下面我們在Flash中調用這個hello方法。
        我使用的是Web 服務類來調用該ws 的hello方法。
        /**
   ActionScript Source File -- Created with SAPIEN Technologies PrimalScript 3.0
   
   @class wsSessionTest_1 
   @package wsSessionTest_1.as
   @author JimLee
   @codehint 
   @example 
   @tooltip 
*/

import mx.services.*;
//匯入services包

var myws:WebService= new WebService("http://localhost/ws/test.asmx?wsdl");
//使用WebService對象myws引用ws 

//定義ws方法hello回調後的處理,參數result是ws的hello方法返回的值
// showSession_lb:lable組件
function hello(result) {
    showSession_lb.text="從ws返回的訊息:"+result;
};

//testSession_bt:按鈕
//使用PengingCall對象op_1在flash中代理ws的hello方法
this.testSession_bt.onPress=function(){
    var op_1:PendingCall=myws. hello ();
    op_1.onResult= hello;
    }

需要注意的是,如果Web 服務類僅限 Flash Professional中使用,如果要在作品中使用Web服務類,必須匯入Web服務類包,不然,是不會有響應的。
依次點擊“視窗-其他面板-公用庫-類”,開啟公用庫“類”,從中拖動WebSessionClasses組件到舞台上,將Web服務類包匯入Fla,然後,你可以刪除舞台上的WebSessionClasses組件的執行個體。

下面就可以測試一下你的swf了,如果不出以外,lable組件showSession_lb中將出現“從ws返回的訊息:你好”。不過,要提醒的是:可要給你的lable組件留夠足夠的長度來顯示資訊啊!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.