参考地址:http://www.cnblogs.com/zhili/p/WebService.htmlhtml
1、WebService概述web
SOAP、WSDL、UDDI
SOAP(Simple Object Access Protocal,简单对象访问协议),是在分散或在分布式环境中交换信息的简单协议。
WSDL(Web Services Description Language,Web服务描述语言) 对WebService 的解释说明文档,描述Web服务发布的XML格式
UDDI 统一描述、发现和集成(Universal Description, Discovery, and Integration)的缩写,是Web服务的黄页,它是一个基于XML的跨平台的描述规范,可使世界范围内的企业在互联网上发布本身所提供的服务供其余客户查询使用。
趣味理解:
Web Service 比如是一个服务供应商;
SOAP 就像是两个公司之间签的合同,约束双方按照必定的规范和标准作事;
WSDL就像说明书,告诉别人你有什么,能给别人提供什么服务;
UDDI比如你的公司须要在黄页或工商注册,方便别人查询ajax
2、WebService执行过程api
3、WebService的优缺点跨域
优势:缓存
1.跨平台:Web Services彻底基于XML(可扩展标记语言)、XSD(XMLSchema)等与平台无关的行业标准。安全
2.自描述:Web Service使用WSDL进行自我描述,包括服务的方法、参数、类型和返回值等相关信息。网络
3.跨防火墙:Web Service使用http协议进行通讯,能够穿越防火墙。分布式
缺点:网站
1.效率低下,不适合作单应用系统的开发。
2.安全问题:Web Services没有自身的安全机制,必须借助Http协议或IIS等宿主程序实现信息安全加密。
4、使用参数介绍
Web Service(Web 服务)提供如下属性。
Namespace:默认是"http://tempuri.org/",此属性的值包含 XML Web Service 的默认命名空间。XML 命名空间提供了一种在 XML 文档中建立名称的方法,该名称可由统一资源标识符(URI)标识。使用XML命名空间,能够惟一标识XML文档中的元素或属性于是,在 XML Web Service 的服务说明中,Namespace 被用作与 XML Web Service 直接相关的XML 元素的默认命名空间。若是不指定命名空间,则使用默认命名空间http://tempuri.org/。
Name:此属性的值包含 XML Web Service 的名称。在默认状况下,该值是实现 XML Web Service 的类的名称。
Description:此属性的值包含描述性消息,此消息将在 XML Web Service 的说明文件(例如服务说明和服务帮助页)生成后显示给 XML Web Service 的潜在用户。
WebMethod(Web 服务方法)有如下 6 个属性。
Description:是对 Web Service 方法的描述信息。就像 Web Service 方法的功能注释,可让调用者看见的注释。
EnableSession:指示 Web Service 是否启动 Session 标志,主要经过 Cookie 完成,默认为 false。
MessageName:主要实现方法重载后的重命名:
TransactionOption:指示 Web Service 方法的事务支持。
CacheDuration:设置响应应在缓存中保留的秒数。这样 Web Service 就不须要重复执行多遍,能够提升访问效率,而 CacheDuration 就是指定缓存时间的属性。
5、代码
TestWebService1.asmx文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; namespace WebService { /// <summary> /// TestWebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://www.anniya.com/api")]// [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要容许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释如下行。 [System.Web.Script.Services.ScriptService] public class TestWebService1 : System.Web.Services.WebService { [WebMethod] [SoapHeader("myHeader")] public string HelloWorld(string name) { if (!myHeader.IsValid()) { return "对不起,您没有权限访问"; } return name + ",Hello World"; } public MySoapHeader myHeader = new MySoapHeader(); [WebMethod] public List<Student> GetStudent() { return new List<Student>() { new Student() {Id = 1, Name = "张三1"}, new Student() {Id = 2, Name = "张三2"}, new Student() {Id = 3, Name = "张三3"} }; } [WebMethod(Description="根据学生列表进行处理")] public List<Student> GetStudentResultByStudentList(List<Student> student) { foreach (var stu in student) { stu.Name += ",已经处理过了"; } return student; } } public class Student { public int Id { get; set; } public string Name { get; set; } } public class MySoapHeader : SoapHeader { public string UserName { get; set; } public string Password { get; set; } public bool IsValid() { if (UserName == "admin" && Password == "123") { return true; } else { return false; } } } }
调用代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using WebApplicationTestWebService.ServiceReference1; namespace WebApplicationTestWebService { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (TestWebService1SoapClient client = new TestWebService1SoapClient()) { MySoapHeader myHeader = new MySoapHeader(); myHeader.UserName = "admin"; myHeader.Password = "123";//用户名和密码要和调用的Web Service 一致 Response.Write(client.HelloWorld(myHeader, "yxl") + "</br>"); Response.Write("<hr/>"); Student[] students = client.GetStudent(); foreach (var student in students) { Response.Write(student.Name+"</br>"); } Response.Write("<hr/>"); foreach (var student in client.GetStudentResultByStudentList(students)) { Response.Write(student.Name + "</br>"); } } } } }
若是使用ajax调用而且跨域的话,能够在本地应用程序中调用webservice,而后在用js调用本地应用程序,这样就不会跨域了
安全除了使用SoapHeader,还可使用SSL,配置SSL网站,还能够控制访问的IP地址,可是ip地址的维护不方便,不推荐使用