The data encoded in the XML document is part of the format. The XML document consists of root ENVELOPE tag, and the tag is composed of the required Body element and an optional Header element. The Body element consists of a message-specific data. An optional HEADER element can contain additional messages that are not directly related to a particular message. Each child element of the Header element is called a SOAP header.
Although the SOAP header can contain data related to the message (because the SOAP specification does not rigorously defining the content of the SOAP header), they typically contain information about the infrastructure processing in the web server.
XML Web Services created using ASP.NET can define and operate the SOAP header. Defining the SOAP header is the class that represents the data in a specific SOAP header and from
SoapHeader
This class is done in the class.
Create a class derived from the SoapHeader class, which matches the root element of the SOAP header.
Public Class MyHeader: SoapHeader
Add public fields or properties to match the names of each element in the SOAP header and their respective data types. For example, in the case of a given SOAP header, the subsequent class defines a class representing the SOAP header.
Public class myheader: soapheader {public string username; public string password;}
Handling SOAP headers in XML Web Services
Add a public member to a class that implements XML Web Services that represents the SOAP header type. [WebService (Namespace = "http://www.contoso.com")] public class MyWebService {// Add a member variable of the type deriving from SoapHeader public MyHeader myHeaderMemberVariable;. SoapHeader attribute is applied to the processing of the SOAP header Each XML Web Services method. Set the MEMBERNAME attribute of the SoapHeader feature to the name of the member variable created in the first step. [WebMethod] [SOAPHEADER ("MyHeadermembervariable")] Public Void MyWebMethod () In each XML Web Services method for applying the SOAPHEADER feature, access the member variable created in the first step to handle data sent in the SOAP header. [WebMethod] [SoapHeader ( "myHeaderMemberVariable")] public void MyWebMethod () {.. // Process the SoapHeader if (myHeaderMemberVariable.Username == "admin") {// Do something interesting}} Example:
MyWebService XML Web Services has a member variable called MyHeaderMemberVariable, which is a MEMBERNAME attribute that is derived from SOAPHEADER (MyHeader) and sets to the SOAPHEADER feature. In addition, the SOAPHEADER feature is applied to the MyWebmethod XML Web Services method for specifying the MyHeaderMembervariable member variable. In the MyWebMethod XML Web Services method, access the MyHeaderMeMberVariable member variable to get the value of the UserName XML element of the SOAP header.
<% @ WebService language = "c #" class = "mywebservice"%> using system.web.services; using system.web.services.protocols;
// define a soap header by deriving from the soapheader base class.public class myheader: soapheader {public string username; public string password;
[WebService (Namespace = "http://www.contoso.com")] public class MyWebService {// Add a member variable of the type deriving from SoapHeader public MyHeader myHeaderMemberVariable;.. // Apply a SoapHeader attribute [WebMethod] [ SoapHeader ( "myHeaderMemberVariable")] public void MyWebMethod () {// process the SoapHeader. if (myHeaderMemberVariable.Username == "admin") {// Do something interesting.}}} SOAP header generation processing when the client When XML Web Services communicates, the XML Web Services client can send and receive SOAP headers. This agent class includes information about the SOAP header when using the WSDL.exe utility to generate an agent class for the XML Web Services that is expected or returned to the SOAP header. Clearly, the agent class has a member variable representing the SOAP header, which is associated with the SOAP header in XML Web Services. The proxy class also has a definition of the corresponding class representing the SOAP header. For example, the proxy class generated for the above XML Web Services will have a MyHeader type member variable and the definition of the MyHeader class.
Create a new instance of the class representing the SOAP header.
MyHeader MysoapHeader = new myheader ();
Fill in the SOAP header.
MysoapHeader.userName = "username"; mysoapheader.password = "password";
Create a new instance of the proxy class.
MyWebservice Proxy = new mywebservice ();
Assign the SOAP header object to a member variable of the proxy class representing the SOAP header.
Proxy.myheadervalue = MySoApHeader The proxy class call method for communication with the XML Web Services method. The SOAP request for the SOAP request sent to XML Web Services will include the contents of the data stored in the SOAP header object.
String results = proxy.mywebmethod ();
The following demonstrates how to pass the SOAP header from the client to XML Web Services.
<% @ Page language = "c #"%>
Void Page_Load (Object O, Eventargs E) {
MyHeader MysoapHeader = new myheader ();
// Populate the values of the SOAP header mySoapHeader.Username = "username";. MySoapHeader.Password = "password"; // Create a new instance of the proxy class MyWebService proxy = new MyWebService ();. // Add the MyHeader SOAP Header to the Soap Request. Proxy.myHeaderValue = MySoApHeader;
// Call The Method on The Proxy Class That Communicates with // Your XML Web Service Method. String Results = Proxy.Mywebmethod ();
// Display the results of the method in a label. ReturnValue.text = results;} script>