服务篇:我的第一WebService应用

一、我的第一个Webservice应用

1.新建一个空项目

2.添加新项,加入asmx,并再浏览器浏览

3.添加一个aspx网页

4.右键引用→添加服务引用→高级→添加Web引用,输入再浏览器浏览的asmx地址→添加引用。

5.再aspx网页加载时:

localhost.WebService1 ws = new localhost.WebService1();
Response.Write(ws.HelloWorld());

 

二、Webservice不完全对外公开,如何验证?

 1.再Webservice下定义一个类:

  public class MySoapHeader:SoapHeader
    {
        public string pwd { get; set; }
        public bool Check (string pwd)
        {
            if(pwd=="123")
            {
                return true;
            }
            else
            {
                return true;
            }
        }
    }

2.在基类下加入代码

    public MySoapHeader header;
        [WebMethod]
        [SoapHeader("header")]
        public string HelloWorld()
        {
            if (header.Check(header.pwd))
            {
                return "Hello World";
            }
            else
            {
                return "你没有权限调用";
            }
            #endregion
        }

3.aspx页面加载时

localhost.WebService1 ws = new localhost.WebService1();
localhost.MySoapHeader header=new localhost.MySoapHeader();
header.pwd = "123";
ws.MySoapHeaderValue = header;
Response.Write(ws.HelloWorld());
posted @ 2016-07-20 16:54  周银胜  阅读(197)  评论(0编辑  收藏  举报