1,客户端访问WebService基础
页面中的内容 :
页面代码:
页面代码:
webService:
asp页面代码:
步骤:新建一个普通的webService
释放webService方法 a,访问级别public;
1,[NameSpaceName].ClassName.MethodName()
2,依次传入参数
下面是简单的例子:
webService中的内容
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- namespace AJAXEnabledWebApplication2
- {
- /// <summary>
- /// WebService1 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolBoxItem(false)]
- //为类添加自己定义属性[ScriptService]
- [System.Web.Script.Services.ScriptService]
- public class WebService1 : System.Web.Services.WebService
- {
- //释放WebService方法
- [WebMethod]
- public int GetRandom()
- {
- return new Random(DateTime.Now.Millisecond).Next();
- }
- [WebMethod]
- public int GetRandomBetween(int min, int max)
- {
- return new Random(DateTime.Now.Millisecond).Next(min, max);
- }
- [WebMethod]
- public void test()
- {
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXEnabledWebApplication2._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <!--Inlinescript为true的时候webservice的方法生成的script代码会释放在页面中,默认为false-->
- <asp:ScriptManager ID="ScriptManager1" runat="server" >
- <Services>
- <asp:ServiceReference Path="WebService1.asmx" Inlinescript="true" />
- </Services>
- </asp:ScriptManager>
- <div>
- <input type="button" value="无返回值" onclick="test()" />
- <input type="button" value="无参数返回一个随机数字" onclick="GetRandom()" />
- <input type="button" value="返回一个0到100之间的随机数字" onclick="GetRandom(0,100)" />
- <script type="text/javascript" language="javascript">
- function GetRandom(max,min){
- //js函数默认的属性,argument 参数信息,一种js重载的实现方法
- if(arguments.length!=2){
- AJAXEnabledWebApplication2.WebService1.GetRandom(successed);
- }
- else{
- AJAXEnabledWebApplication2.WebService1.GetRandomBetween(max,min,successed);
- }
- }
- function test(){
- //无返回值,回调函数会弹出个null值
- AJAXEnabledWebApplication2.WebService1.Test(successed);
- }
- function successed(result){
- alert(result);
- }
- </script>
- </div>
- </form>
- </body>
- </html>
2,客户端访问PageMethod基础
服务器端步骤:客户端对应的服务器段的aspx页面中定义
公开的静态方法
方法上面使用[WebMethod]Attribute
ScriptManager的EnablePageMethod属性为true
简单例子:
后台类:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- namespace AJAXEnabledWebApplication2
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //向客户端释放方法
- [System.Web.Services.WebMethod]
- //public类型的static
- public static DateTime GetDateTimeNow()
- {
- return DateTime.UtcNow;
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AJAXEnabledWebApplication2.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <!---ScriptManager的 EnablePageMethods="true"->
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
- </asp:ScriptManager>
- <input type="button" value="调用pageMethod方法" onclick="GetDateTimeNow()" />
- <script type="text/javascript" language="javascript">
- function GetDateTimeNow(){
- //PageMethods.释放的方法名字
- PageMethods.GetDateTimeNow(seccessed);
- }
- function seccessed(result){
- alert(result);
- }
- </script>
- </form>
- </body>
- </html>
3,错误处理
错误信息类:Sys.Net.WebServicError
Sys.Net.WebServicError类提供了 timeout ,message,exceptionType,stackTrace 4个属性
例子:
webService代码:
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- namespace AJAXEnabledWebApplication2
- {
- /// <summary>
- /// Error 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolBoxItem(false)]
- [System.Web.Script.Services.ScriptService]
- public class Error : System.Web.Services.WebService
- {
- [WebMethod]
- public int GetDivsion(int a, int b)
- {
- return a / b;
- }
- [WebMethod]
- public int Timeout()
- {
- //设置等待5秒后返回值
- System.Threading.Thread.Sleep(5000);
- return 0;
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="AJAXEnabledWebApplication2.WebForm2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <asp:ServiceReference Path="Error.asmx" />
- </Services>
- </asp:ScriptManager>
- <input id="Button1" type="button" value="调用一个除零函数抛出异常" onclick="getDivsion(4,0)" />
- <input id="Button2" type="button" value="调用一个除非零函数得到结果" onclick="getDivsion(4,4)" />
- <input id="Button3" type="button" value="触发一个超时的错误" onclick="getTimeout()" />
- <script type="text/javascript" language="javascript">
- function getDivsion(a,b){
- //参数后跟成功后的回调函数,和错误的回调函数
- AJAXEnabledWebApplication2.Error.GetDivsion(a,b,seccessed,Failed);
- }
- function getTimeout(){
- //这里设置等待服务器多久还没有返回数据 就给客户端提醒超时了 服务器中设置了5秒,等俩秒后会返回超时,如果设置为6000则正常返回1
- AJAXEnabledWebApplication2.Error.set_timeout(2000);
- AJAXEnabledWebApplication2.Error.Timeout(seccessed,Failed);
- }
- //调用成功的回调函数
- function seccessed(result){
- alert(result);
- }
- //调用失败的回调函数
- function Failed(error){
- var message=String.format("Timeout:{0}/n Message:{1}/n exceptionType:{2}/n stackTrace:{3}",
- error.get_timedOut(),
- error.get_message(),
- error.get_exceptionType(),
- error.get_stackTrace()
- );
- alert(message);
- }
- </script>
- </form>
- </body>
- </html>
4,复杂数据类型使用基础
1,定义了自己的数据类型时候,类型的一些共有属性和字段都被释放(返回值)和接受(参数)
2,容器对象
--实现了 IList接口的对象
--实现了IDictionary接口的对象 (客户端是用一个对象来模拟字典的,所以字典数据中的key必须是string类型)
例子:
自定义类:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- namespace AJAXEnabledWebApplication2
- {
- public class Person
- {
- public string firstName;
- public string lastName;
- public int age;
- public string FullName
- {
- get
- {
- return firstName + " " + lastName;
- }
- }
- public override string ToString()
- {
- return "Name:"+this.FullName+" Age:"+age.ToString();
- }
- }
- }
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- namespace AJAXEnabledWebApplication2
- {
- /// <summary>
- /// WebService2 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolBoxItem(false)]
- [System.Web.Script.Services.ScriptService]
- public class WebService2 : System.Web.Services.WebService
- {
- [WebMethod]
- //一个人的年龄增加了一岁
- public Person AddAge(Person p)
- {
- p.age += 1;
- return p;
- }
- [WebMethod]
- //数组
- public System.Collections.Generic.List<int> Reverse(System.Collections.Generic.List<int> list)
- {
- //反转数组
- list.Reverse();
- return list;
- }
- [WebMethod]
- ////数据字典
- public System.Collections.Generic.IDictionary<string, Person> GetPersons()
- {
- System.Collections.Generic.Dictionary<string, Person> result = new System.Collections.Generic.Dictionary<string, Person>();
- Person p1 = new Person();
- Person p2 = new Person();
- p1.firstName = "Si";
- p1.lastName = "Li";
- p1.age = 23;
- result[p1.FullName] = p1;
- p2.age = 25;
- p2.firstName = "Wu";
- p2.lastName = "Wang";
- result[p2.FullName] = p2;
- return result;
- }
- }
- }
aspx页面
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="AJAXEnabledWebApplication2.WebForm3" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <asp:ServiceReference Path="WebService2.asmx" />
- </Services>
- </asp:ScriptManager>
- <input id="Button1" type="button" value="年龄增加一岁" onclick="AddAge()" />
- <input id="Button2" type="button" value="反转数组[1,2,3,4,5,6s]" onclick="revers([1,6])" />
- <input id="Button3" type="button" value="返回一个数据字典数据" onclick="getPersons()" />
- <script language="javascript" type="text/javascript">
- //年龄增加一岁
- function AddAge(){
- var person=new Object();
- person.firstName="Stone";
- person.lastName="Zhang";
- person.age=23;
- //调用webservice中的年龄增加一岁的方法
- AJAXEnabledWebApplication2.WebService2.AddAge(person,AddAgeseccessed);
- }
- function AddAgeseccessed(result){
- //结果是24
- var message=String.format("FirstName:{0}/n LastName:{1}/nFullName:{2}/nAge:{3}",
- result.firstName,
- result.lastName,
- result.FullName,
- result.age
- );
- alert(message);
- }
- //数组
- function revers(array){
- AJAXEnabledWebApplication2.WebService2.Reverse(array,seccessed);
- }
- function seccessed(result){
- alert(result);
- }
- //数据字典
- function getPersons(){
- AJAXEnabledWebApplication2.WebService2.GetPersons(getPersonsSeccssed);
- }
- function getPersonsSeccssed(result){
- //枚举字典中的每一项
- //这里得不到字典对象中的方法
- for(var key in result){
- alert(key+": "+result[key].age);
- }
- }
- </script>
- </form>
- </body>
- </html>
5,客户端代理使用基础
Invoke(arg1,...,argN,onSucceeded,onFailed,userContext)
回调函数完整签名:
onSucceeded(result,userContext,methodName)
onFailed(error,methodName)
error对象
-timeout 比较常用
-defaultUserContext
-defaultSucceededCallBack
例子:
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- namespace AJAXEnabledWebApplication2
- {
- /// <summary>
- /// Error 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolBoxItem(false)]
- [System.Web.Script.Services.ScriptService]
- public class Error : System.Web.Services.WebService
- {
- [WebMethod]
- public int GetDivsion(int a, int b)
- {
- return a / b;
- }
- [WebMethod]
- public int Timeout()
- {
- //设置等待5秒后返回值
- System.Threading.Thread.Sleep(5000);
- return 0;
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="AJAXEnabledWebApplication2.WebForm4" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <asp:ServiceReference Path="Error.asmx" />
- </Services>
- </asp:ScriptManager>
- <input id="Button1" type="button" value="调用一个除零函数抛出异常" onclick="getDivsion(4,0)" />
- <input id="Button2" type="button" value="调用一个除非零函数得到结果" onclick="getDivsion(4,4)" />
- <input id="Button3" type="button" value="触发一个超时的错误" onclick="getTimeout()" />
- <script type="text/javascript" language="javascript">
- //webmethod默认属性
- AJAXEnabledWebApplication2.Error.set_defaultFailedCallback(Failed);
- AJAXEnabledWebApplication2.Error.set_timeout(2000);
- function getDivsion(a,b){
- //参数后跟成功后的回调函数,和错误的回调函数
- // AJAXEnabledWebApplication2.Error.GetDivsion(a,Failed);
- //去掉了上面的出错的回调函数,系统会默认找set_defaultFailedCallBack()
- AJAXEnabledWebApplication2.Error.GetDivsion(a,seccessed);
- }
- function getTimeout(){
- //这里设置等待服务器多久还没有返回数据 就给客户端提醒超时了 服务器中设置了5秒,等俩秒后会返回超时,如果设置为6000则正常返回1
- // AJAXEnabledWebApplication2.Error.set_timeout(2000);
- // AJAXEnabledWebApplication2.Error.Timeout(seccessed,Failed);
- AJAXEnabledWebApplication2.Error.Timeout(seccessed);
- }
- //调用成功的回调函数
- function seccessed(result){
- alert(result);
- }
- //调用失败的回调函数
- function Failed(error){
- var message=String.format("Timeout:{0}/n Message:{1}/n exceptionType:{2}/n stackTrace:{3}",
- error.get_stackTrace()
- );
- alert(message);
- }
- </script>
- </form>
- </body>
- </html>
目前就了解了这些总结下~
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。