zoukankan      html  css  js  c++  java
  • WCF分布式开发必备知识(3):Web Service 使用

    参考地址:http://www.cnblogs.com/zhili/p/WebService.html

    一、WebService概述

    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好比你的公司需要在黄页或工商注册,方便别人查询

    二、WebService执行过程

    • 在客户端上,创建了一个Web Services代理类的实例。该对象驻留在客户端机器上。
    • 客户端调用代理类上的方法
    • 客户端机器将Web Services方法的参数序列化为SOAP消息,然后通过传送协议发送给Web Services。
    • Web Services底层结构接收SOAP消息并进行反序列化。它会创建Web Services的类的实例,同时调用对应的Web Services方法。
    • Web Services方法执行,并返回结果。
    • Web Services底层结构会将返回结果序列化为SOAP消息,然后通过网络发送回客户端。
    • 客户端将接收SOAP消息,然后将XML反序列为返回值或任何输出参数,并将它们传递给代理类的实例。
    • 客户端接收返回值和所有输出参数。

    三、WebService的优缺点

    优点:

    1.跨平台:Web Services完全基于XML(可扩展标记语言)、XSD(XMLSchema)等与平台无关的行业标准。

    2.自描述:Web Service使用WSDL进行自我描述,包括服务的方法、参数、类型和返回值等相关信息。

    3.跨防火墙:Web Service使用http协议进行通信,可以穿越防火墙。

    缺点:

    1.效率低下,不适合做单应用系统的开发。

    2.安全问题:Web Services没有自身的安全机制,必须借助Http协议或IIS等宿主程序实现信息安全加密。

    四、使用参数介绍

    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 就是指定缓存时间的属性。

    五、代码

    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地址的维护不方便,不推荐使用

  • 相关阅读:
    使用过Redis,我竟然还不知道Rdb
    系统的讲解
    系统的讲解
    我眼中的 RPC
    Swoole Timer 的应用
    场景调研
    二维数组环求最大子数组
    《你的灯亮着吗》 阅读笔记三
    《你的灯亮着吗》 阅读笔记二
    《你的灯亮着吗》阅读笔记一
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3861133.html
Copyright © 2011-2022 走看看