asp.net webService add the header file validation

The first step, the new asp.net project

2. Add MyTest ws Service

code show as below

2.1 The establishment of header files need to verify that the entity classes

. 1  the using the System;
 2  the using the System.Collections.Generic;
 . 3  the using the System.Linq;
 . 4  the using the System.Web;
 . 5  the using System.Web.Services.Protocols;
 . 6  
. 7  namespace Aspheadnet.Models
 . 8  {
 . 9      public  class UserSoapHeader: the SoapHeader
 10      {
 . 11          Private  String _username;
 12 is  
13 is          Private  String _pwd;
 14  
15          // public property will automatically generate xml node 
16  
. 17         public string UserName
18         {
19 
20             get { return _userName; }
21 
22             set { _userName = value; }
23 
24         }
25         public string Pwd
26         {
27 
28             get { return _pwd; }
29 
30             set { _pwd = value; }
31 
32         }
33     }
34 }

2.2 write ws Service

 1 using Aspheadnet.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Services;
 7 using System.Web.Services.Protocols;
 8 
 9 namespace Aspheadnet
10 {
11     /// <summary>
12     /// MyTest 的摘要说明
13     /// </summary>
14     [WebService(Namespace = "http://tempuri.org/")]
 15      [WebServiceBinding (conformsTo = WsiProfiles.BasicProfile1_1)]
 16      [System.ComponentModel.ToolboxItem ( false )]
 17      // To call this allows the use of ASP.NET AJAX Web service from a script, uncomment the following line. 
18 is      // [System.Web.Script.Services.ScriptService] 
. 19      public  class the MyTest: the System.Web.Services.WebService
 20 is      {
 21 is  
22 is          // this attribute as an attribute validation
 23 is          // SoapHeaderAttribute method in this variable name consistent with 
24          public UserSoapHeader userHeader;
 25  
26 is          [the WebMethod]
 27         [The SoapHeader ( " userHeader " )] // very important here, and to verify the name of the same name attribute definitions! 
28          public  String the HelloWorld ()
 29          {
 30              // After entering this method, userHeader will automatically have the value of 
31 is              IF (userHeader! = null )
 32              {
 33 is                  IF (userHeader.UserName =! " Joe Smith " )
 34 is                  {
 35                      return  " user name does not match " ;
 36                  }
 37 [                  IF ! (= userHeader.Pwd "123 " )
 38 is                  {
 39                      return  " passwords do not match " ;
 40                  }
 41 is  
42 is                  return  " request was successful " ;
 43 is  
44 is              }
 45  
46 is              return  " Invalid Request " ;
 47          }
 48      }
 49 }

Step 3 client calls

Add Service Reference 3.1 webservice

1     <client>
2       <endpoint address="http://192.168.21.195:8033/MyTest.asmx" binding="basicHttpBinding"
3         bindingConfiguration="MyTestSoap" contract="TestService.MyTestSoap"
4         name="MyTestSoap" />
5     </client>

Here I released a local test environment iis

3.2 interface for writing to the relevant code

        protected void Button1_Click(object sender, EventArgs e)
        {
            test();
        }

        void test()
        {
            client.TestService.MyTestSoapClient mc = new MyTestSoapClient();

            WebService s = new WebService();
            UserSoapHeader a = new UserSoapHeader();

            a.username = " John Doe " ;

            a.Pwd = "123";

            Response.Write(mc.HelloWorld(a)); 
        }

Results are as follows

 

Description file request was successful too far certified.

 

  3.2 If a header file is not involved in the argument, or parameter passed in error will be invalid request.

 

This document describes how to crawl below the button events this page.

Guess you like

Origin www.cnblogs.com/suntanyong88/p/12426558.html