This article shares the steps for calling web services on the page to verify using SOAP header for your reference. The specific content is as follows
first step:The class used for SOAP verification must be derived from the SoapHeader class. The Public properties in the class will appear in the automatically generated XML node, that is:
<soap:Header> <UserSoapHeader xmlns="/"> <UserName>string</UserName> <Pwd>string</Pwd> </UserSoapHeader> </soap:Header> public class UserSoapHeader : SoapHeader { private string _userName; private string _pwd; //The public attribute will automatically generate xml nodes public string UserName { get { return _userName; } set { _userName = value; } } public string Pwd { get { return _pwd; } set { _pwd = value; } } }
Step 2:
Add a public property (must be public) in the WebServices service class, with type from UserSoapHeader
/// <summary> /// Summary description of WebService/// </summary> [WebService(Namespace = "/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebService : { //This property will be used as a verification property//The name in the SoapHeaderAttribute of the method is consistent with this variable public UserSoapHeader userHeader; public WebService() { //If using the designed component, please uncomment the following line //InitializeComponent(); } [WebMethod] [SoapHeader("userHeader")]//It is very important here, the name must be consistent with the defined verification attribute name! public string HelloWorld() { //After entering this method, userHeader will have a value automatically if (userHeader != null) { return "this is retVal : " + ; } return " check not successed "; } }
Step 3: Make calls on the client:
1. Add WEB reference
2. Instantiated service class
3. Instantiate the SOAP header (the attribute that will be automatically generated for verification on the client side; the attribute type is: UserSoapHeader; the name of the attribute is: UserSoapHeaderValue); the automatically generated attribute generation rules are: verification type name +Value;
4. Call the service-provided method.
WebService s = new WebService(); UserSoapHeader a = new UserSoapHeader(); = "admin"; = "zz"; = a; //This property is automatically generated ( () ); // this is retVal : admin
It's very simple. I hope everyone can master the steps of using soapheader as verification. Thank you for your reading.