C#/.NET WebService的一步步创建使用及访问验证

C#/.NET WebService的一步步创建使用及访问验证

废话就不多说了,直接开始建工程

1、首先创建MVC项目

在这里插入图片描述

2、在工程目录下创建Remote文件夹(使用规范)

在这里插入图片描述

3、文件夹中添加【Web服务(asmx)】新建项

在这里插入图片描述

4、在axmx文件中写接口代码—Web方法要有WebMethod标记

        [WebMethod]//必须标记
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]//必须标记
        public int Plus(int x, int y)
        {
            return x + y;
        }
        //没扩展的是无法找到的
        public int Minus(int x, int y)
        {
            return x - y;
        }

5、新建Uinit单元测试项目

6、添加服务引用:右键打开【Web服务(asmx)】文件在浏览器中运行,然后复制Url,再粘贴至服务引用中

先打开【Web服务(asmx)】文件

在这里插入图片描述

在这里插入图片描述

添加服务引用

在这里插入图片描述
在这里插入图片描述

7、最后using(xxx.xxClient client = new xxx.xxClient()),后面就是正常用了。

        [TestMethod]//测试方法
        public void TestMethod()
        {
            using (WebServiceTest.WebServiceSoapClient client = new WebServiceTest.WebServiceSoapClient())
            {
                var result1 = client.HelloWorld();

                var result2 = client.Plus(123, 345);
            }
        }

扩展:

要传回List数据:

在已经添加的服务引用右键->配置服务引用->集合类型选择Generic.List
在这里插入图片描述

添加访问验证:

在上面Remote文件夹下建CustomSoapHeader类

    /// <summary>
    /// Header:分配个加密钥  账号密码加密
    /// 
    /// </summary>
    public class CustomSoapHeader : System.Web.Services.Protocols.SoapHeader
    {

        private string userName = string.Empty;
        private string passWord = string.Empty;
        public CustomSoapHeader()//必须有一个无参数的构造函数
        { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        public CustomSoapHeader(string userName, string passWord)
        {
            this.userName = userName;
            this.passWord = passWord;
        }

        /// <summary>
        /// 获取或设置用户用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { this.userName = value; }
        }

        /// <summary>
        /// 获取或设置用户密码
        /// </summary>
        public string PassWord
        {
            get { return passWord; }
            set { this.passWord = value; }
        }
        public bool Validate()
        {
            return this.UserName.Contains("s") && this.PassWord.Contains("1");
        }

    }
Web代码:
        [WebMethod]
        [System.Web.Services.Protocols.SoapHeader("CustomSoapHeader")]
        public List<WebServiceUser> Get()
        {
            if (this.CustomSoapHeader != null && this.CustomSoapHeader.Validate())
            {
                return new List<WebServiceUser>()
                {
                    new WebServiceUser()
                    {
                        Id=123,
                        Age=23,
                        Sex=0,
                        Name="MrSorry",
                        Description="高级班的学员1"
                    },
                    new WebServiceUser()
                    {
                        Id=234,
                        Age=34,
                        Sex=0,
                        Name="专注",
                        Description="高级班的学员2"
                    },
                };
            }
            else
            {
                throw new SoapException("身份验证不通过", SoapHeaderException.ServerFaultCode);
            }
        }
    public class WebServiceUser
    {
        public int Id { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
访问代码:

        [TestMethod]//测试方法
        public void TestMethod()
        {
            using (WebServiceTest.WebServiceSoapClient client = new WebServiceTest.WebServiceSoapClient())
            {
                var result3 = client.Get(new WebServiceTest.CustomSoapHeader()
                {
                    UserName = "s400",
                    PassWord = "1232433"
                });
            }

        }
发布了190 篇原创文章 · 获赞 298 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/qq_34202873/article/details/94719381