在Flash中调用WS时的身份验证_3:基本数据类型

在ws和flash客户端之间进行调用,最令人烦恼的莫过于两边同时支持的数据类型太少,下面,我们扩展一下前面的例子,看一看他们之间都可以同时使用那些数据类型。
首先我们来看一看Number类型:
XML 架构类型支持的数字简单类型很多,在flash中都作为Number类型处理。下面我们对sample2.asmx.cs进行小小的修改:增加一个方法IncAB,如下:
        [WebMethod]
        public int IncAB(int A,int B){
            return A+B;
            }
此图片仅显示局部,请点击查看完整图片
200410716233386082.jpg
在flash的web服务窗口中可以查看IncAB方法的参数和返回值的类型都是整型。
对wsSample_2.as也作一点小小的修改:

function IncAB(result){
    trace("ws方法IncAB返回的值为:");
    trace("///");
    trace(result);
    }

    ……
this.incAB_bt.onPress=function(){
    var a:Number=34;
    var b:Number=56;
    var op_2:PendingCall=myws.IncAB(a,b);
    op_2.onResult=IncAB;
    }

 测试一下,output窗口会返回
ws方法IncAB返回的值为:
///
90

如果我们将a和b的值分别设置为一个小数了?比如:56.4之类?output将返回
Error opening URL  http://localhost/ws/sample2.asmx
如果你想让提示更加友好,web服务类提供了容错处理,但是不是我们这里要讨论的内容,后面再说。
    对于其他的数字简单类型,都和这个差不多,可以自己尝试一下。
    
    下面是扩充后的sample2.asmx.cs文件,添加了IncAB、GetNow、PassDate、PassBool这四个web方法:
/* ======================================================================

C# Source File -- Created with SAPIEN Technologies Primalcode 3.0

NAME: sample2.asmx.cs

AUTHOR: JimLee , Dxl School
DATE  : 2004-10-7

COMMENT: 这个例子演示了在ws接受来自客户端的数字简单类型、日期和时间简单类
         型、bool类型、名称和字符串简单类型以及Array类型参数并返值。

====================================================================== */

using System;
using System.Web;
using System.Web.Services;

namespace wsLearn{
    [WebService(Namespace=" http://www.dxlschool.com/ws/",Description="例2,演示ws方法的传值",Name="s2") ]
    public class sample2:System.Web.Services.WebService{
        public sample2(){
            //
            }
            
        [WebMethod]
        public string GetUserName(string tName){
            return "你好,"+tName;
            }
            
        [WebMethod]
        public int IncAB(int A,int B){
            return A+B;
            }
            
        [WebMethod]
        public DateTime GetNow(){
            return DateTime.Now;
            }
            
        [WebMethod]
        public DateTime PassDate(DateTime dt){
            return dt;
            }
            
        [WebMethod]
        public bool PassBool(bool bl){
            return bl;
            }
        }
        
    }

   扩充后的wsSample_2.as:
/**
   ActionScript Class File -- Created with SAPIEN Technologies PrimalScript 3.0
   
   @class wsSample_2 
   @package wsSample_2.as
   @author JimLee
   @codehint 
   @example 
   @tooltip 
*/

import mx.services.*;

var myws:WebService=new WebService(" http://localhost/ws/sample2.asmx ?wsdl");

function GetUserName(result){
    trace("ws返回的值为:");
    trace("///");
    trace(result);
    }
    
function IncAB(result){
    trace("ws方法IncAB返回的值为:");
    trace("///");
    trace(result);
    }

function GetNow(result){
    trace("ws方法GetNow返回的值为:");
    trace("/");
    trace(result);
    }
    
function PassDate(result){
    trace("ws方法PassDate返回的值为:");
    trace("/");
    trace(result);
    }
    
function PassBool(result){
    trace("ws方法PassBool返回的值为:");
    trace("/");
    trace(result);
    }
    
this.GetUN_bt.onPress=function(){    
    var op_1:PendingCall=myws.GetUserName("乔狗!!");
    op_1.onResult=GetUserName;
    }        

this.incAB_bt.onPress=function(){
    var a:Number=34;
    var b:Number=56;
    var op_2:PendingCall=myws.IncAB(a,b);
    op_2.onResult=IncAB;
    }
    
this.GetNow_bt.onPress=function(){
    var op_3:PendingCall=myws.GetNow();
    op_3.onResult=GetNow;
    }
    
this.PassDT_bt.onPress=function(){
    var dt:Date=new Date (77, 4, 2);
    var op_4:PendingCall=myws.PassDate(dt);
    op_4.onResult=PassDate;
    }

this.PassBool_bt.onPress=function(){
    var bl:Boolean=true;
    var op_5:PendingCall=myws.PassBool(bl);
    op_5.onResult=PassBool;
    }

例子很简单,不一一注释了。

转载于:https://www.cnblogs.com/savageworld/archive/2006/08/20/481638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值