Flexible definition and processing of SOAP header messages

Source: Internet
Author: User
Tags define definition header soap web services xmlns client
The data encoded in the XML document is part of the format. An XML document consists of a root EnvelopeTag, which is also made up of the required BodyElement and an optional Headerelements are composed. BodyElements are made up of message-specific data. Optional HeaderElements can contain additional messages that are not directly related to a particular message. HeaderEach child element of an element is called a SOAP header.

Although SOAP headers can contain data related to the message (because the SOAP specification does not strictly define the contents of the SOAP header), they typically contain information that is processed by the infrastructure in the WEB server.

XML Web Services created using ASP.net can define and manipulate SOAP headers. Defining a SOAP header is done by defining the class that represents the data in a particular SOAP header and deriving it from the SoapHeader class.

Creates a class that derives from the SoapHeader class whose name matches the root element of the SOAP header.

public class Myheader:soapheader

Add a public field or property that matches the name of each element in the SOAP header and their respective data types.
For example, given the following SOAP headers, the subsequent class defines a class that represents the SOAP header.

<soap:header xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" >
<myheader xmlns= "http://www.contoso.com" >
<Username>MyUsername</Username>
<Password>MyPassword</Password>
</MyHeader>
</soap:Header>


public class Myheader:soapheader
{
public string Username;
public string Password;
}

Handling SOAP headers in XML Web services

Adds a public member to the class that implements the XML Web services that represent the SOAP header type.
[WebService (namespace= "http://www.contoso.com")]
public class MyWebService
{
ADD A is variable of the type deriving from SoapHeader.
Public MyHeader myheadermembervariable;

Apply the SoapHeader attribute to each XML Web services method that you want to process the SOAP header. Set the MemberName property of the SoapHeader attribute to the name of the member variable created in the first step.
[WebMethod]
[SoapHeader ("myheadermembervariable")]
public void MyWebMethod ()

In each XML Web services method that applies the SoapHeader attribute, the member variable created in the first step is accessed to handle the data sent in the SOAP header.
[WebMethod]
[SoapHeader ("myheadermembervariable")]
public void MyWebMethod ()
{
Process the SoapHeader.
if (myheadermembervariable.username = "admin")
{
Do something interesting.
}
}

Instance:

MyWebService XML Web Services has a member variable named myheadermembervariable that belongs to a type derived from SoapHeader (MyHeader) and set to the SoapHeader attribute. MemberName property. In addition, apply the SoapHeader attribute to the MyWebMethod XML Web Services method that specifies the myheadermembervariable member variable. In the MyWebMethod XML Web services method, the myheadermembervariable member variable is accessed to obtain the value of the SOAP header's Username XML element.

<%@ 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.
       }
   }
}

Generating clients that handle SOAP headers
When communicating with XML Web services, the XML Web services client can send and receive SOAP headers. When using the Wsdl.exe utility to generate a proxy class for an XML Web service that expects or returns a SOAP header, the proxy class includes information about the SOAP header. Specifically, the proxy class has member variables that represent SOAP headers that are associated with the SOAP headers in XML Web services. The proxy class also has the definition of the corresponding class that represents the SOAP header. For example, the proxy class generated for the above XML Web services will have a member variable of type MyHeader and a definition for the MyHeader class.

Creates a new instance of the class that represents the SOAP header.

MyHeader Mysoapheader = new MyHeader ();

Fills the value for the SOAP header.

Mysoapheader.username = "Username";
Mysoapheader.password = "Password";

Creates a new instance of the proxy class.

MyWebService proxy = new MyWebService ();

Assigns the SOAP header object to the member variable of the proxy class that represents the SOAP header.

Proxy. Myheadervalue = Mysoapheader
Invokes a method on the proxy class that communicates with the XML Web services method.
The SOAP header portion of a SOAP request sent to an XML WEB services will include the contents of the data stored in the SOAP header object.

String results = Proxy. MyWebMethod ();

The following shows how to pass a SOAP header from the client to an XML Web services.

<%@ Page language= "C #"%>

<asp:label id= "returnvalue" runat= "Server"/>
<script Runat=server 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 so communicates with
Your XML Web service method.
String results = Proxy. MyWebMethod ();

Display the results of the method in a label.
Returnvalue.text = results;
}
</script>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.