• 在WebService中使用简单的自定义SoapHeader


    1.新建专案及加入Web服务 WebServiceA
    2.在WebServiceA.cs中加入新的public class MySOAPHeader
    3.修改WebServiceA的HelloWorld() 添加SoapHeader
    4.加入Web引用 及 新建测试页面
    5.测试页面的按钮事件
    ================================

    1.新建专案及加入Web服务 WebServiceA

    2.在WebServiceA.cs中加入新的public class MySOAPHeader
    -----------------------------------------------------
    public class MySOAPHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string UserPWD;
        public string UserName;

        public bool IsValidUser()
        {
            string strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userName", "md5");
            string strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userPWD", "md5");
            if ( (UserName == strUserName) && (UserPWD == strUserPWD))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    3.修改WebServiceA的HelloWorld() 添加SoapHeader
    ----------------------------------------------
    /// <summary>
    /// WebServiceA 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebServiceA : System.Web.Services.WebService {

        public WebServiceA ()
        {
            //如果使用设计的组件,请取消注释以下行
            //InitializeComponent();
        }

        public MySOAPHeader userSOAPHeader ;

        [WebMethod, SoapHeader("userSOAPHeader")]   
        public string HelloWorld()
        {
            if (userSOAPHeader.IsValidUser())
            {
                return "Hello World";
            }
            else
            {
                return "Sorry";
            }
        }   
    }


    4.加入Web引用 及 新建测试页面

    5.测试页面的按钮事件
    -------------------
    protected void Button1_Click(object sender, EventArgs e)
    {
        //
        string strUserName = this.txt_UserName.Text.Trim();
        strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserName, "md5");
        string strUserPWD = this.txt_UserPWD.Text.Trim();
        strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPWD, "md5");
        //
        localhost.WebServiceA wsA = new localhost.WebServiceA();
        //
        localhost.MySOAPHeader userSOAPHeader = new localhost.MySOAPHeader();
        userSOAPHeader.UserName = strUserName;
        userSOAPHeader.UserPWD = strUserPWD;
        //
        wsA.MySOAPHeaderValue = userSOAPHeader;
        string strReturn = wsA.HelloWorld();
        //
        this.TextBox1.Text = strReturn;
    }

  • 相关阅读:
    Sql Server--如何自动备份数据
    Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AI" in the equal to operation.
    GET方法和POST方法的区别,Get方法到底可传递的字符串的最大长度是多少?
    Specialization For SCCM
    Fullscreen API:全屏操作
    How to enable remote connections to SQL Server
    Advanced Installer
    ajax跨域请求webservice webconfig配置
    SharePoint Resize app
    Sharepoint 开启App 配置App
  • 原文地址:https://www.cnblogs.com/freeliver54/p/763546.html
Copyright © 2020-2023  润新知