Web服务是什么
Web服务的宗旨是创建不需要用户界面就能与其他应用程序交互的Web应用程序。它是松耦合的,并与服务器端和客户端所使用的操作系统、编程语言都无关,这样就在一定的情况下就会使你的效率大大提高。但创建Web服务必须保证的是,服务器端和客户端都要支持行业标准协议HTTP、SOAP(simple object access protocal 简单对象访问协议)和XML。
简单的例子
首先在要先创建web服务,就是.asmx后虚名的文件。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace Learning { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { //构造并填充info数组 string[,] strStuInfo ={ {"200511020120","贰零零五届","人文法律学院","社会工作","(1)班","无悔","1"}, {"200511020121","贰零零五届","人文法律学院","社会工作","(1)班","梁需","2"}, {"200511010122","贰零零五届","人文法律学院","法律专业","(1)班","陆磊","3"}, {"200511010220","贰零零五届","人文法律学院","法律专业","(2)班","白灵","4"}, {"200511010221","贰零零五届","人文法律学院","法律专业","(2)班","剑付","5"}, {"200511020222","贰零零五届","人文法律学院","社会工作","(2)班","敬意","6"}, {"200511100120","贰零零五届","化工生物学院","生物制药","(1)班","黄兴","7"}, {"200511100221","贰零零五届","化工生物学院","生物制药","(2)班","蕾蕾","8"}, {"200511100322","贰零零五届","化工生物学院","生物制药","(3)班","白冰","9"}, {"200611120120","贰零零六届","化工生物学院","发酵工程","(1)班","书并","10"}, {"200611120121","贰零零六届","化工生物学院","发酵工程","(1)班","小鱼","11"}, {"200611120421","贰零零六届","化工生物学院","发酵工程","(4)班","声声","12"}, }; [WebMethod(Description = "根据学号获得学生的姓名")] public string GetName(string strStuNum) { for (int i = 0; i < strStuInfo.GetLength(0); i++) { if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0) return strStuInfo[i, 5].ToString(); } return "您输入的学号不存在"; } [WebMethod(Description = "根据学号获得学生的专业和班级")] public string GetClass(string strStuNum) { for (int i = 0; i < strStuInfo.GetLength(0); i++) { if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0) { return strStuInfo[i, 3].ToString() + strStuInfo[i, 4].ToString(); } } return "您输入的学号不存在"; } [WebMethod(Description = "根据学号获得学生的入学时间")] public string GetYear(string strStuNum) { for (int i = 0; i < strStuInfo.GetLength(0); i++) { if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0) { return strStuInfo[i, 1].ToString(); } } return "您输入的学号不存在"; } [WebMethod(Description = "根据学号获得学生的学院")] public string GetCollege(string strStuNum) { for (int i = 0; i < strStuInfo.GetLength(0); i++) { if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0) { return strStuInfo[i, 2].ToString(); } } return "您输入的学号不存在"; } [WebMethod(Description = "点击次数", EnableSession = true)] public int HitCounter() { if (Session["HitCounter"] == null) { Session["HitCounter"] = 1; } else { Session["HitCounter"] = ((int)Session["HitCounter"]) + 1; } return ((int)Session["HitCounter"]); } } }