How to: Build a Client That Processes SOAP Headers
This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.
Code Example
A Web services client can send and receive SOAP headers when communicating with a Web service. When a proxy class is generated for a Web service expecting or returning SOAP headers using the Wsdl.exe utility, the proxy class includes information about the SOAP headers. Specifically, the proxy class has member variables representing the SOAP headers that correlate to those found in the Web service. The proxy class also has definitions for the corresponding classes representing the SOAP header. For example, proxy classes generated for the previous Web service will have a member variable of type MyHeader
and a definition for the MyHeader
class. For details on creating a proxy class, see Creating an XML Web Service Proxy.
Note: If the Web service defines the member variables representing the SOAP headers of type SoapHeader or SoapUnknownHeader instead of a class deriving from SoapHeader, a proxy class will not have any information about that SOAP header.
To process SOAP headers within a Web service client
Create a new instance of the class representing the SOAP header.
Dim mySoapHeader As MyHeader = New MyHeader()
MyHeader mySoapHeader = new MyHeader();
Populate the values for the SOAP header.
mySoapHeader.Username = UserName mySoapHeader.Password = SecurelyStoredPassword
mySoapHeader.Username = Username; mySoapHeader.Password = SecurelyStoredPassword
Create a new instance of the proxy class.
Dim proxy As MyWebService = New MyWebService()
MyWebService proxy = new MyWebService();
Assign the SOAP header object to the member variable of the proxy class representing the SOAP header.
proxy.MyHeaderValue = mySoapHeader
proxy.MyHeaderValue = mySoapHeader
Call the method on the proxy class that communicates with the Web service method.
The SOAP header portion of the SOAP request sent to the Web service will include the contents of the data stored in the SOAP header object.
Dim results as String = proxy.MyWebMethod()
string results = proxy.MyWebMethod();
Example
The following code example demonstrates how to pass a SOAP header from a client to a Web service.
<%@ Page Language="VB" %>
<asp:Label id="ReturnValue" runat="server" />
<script runat=server language=VB>
Sub Page_Load(o As Object, e As EventArgs)
Dim mySoapHeader As MyHeader = New MyHeader() ' Populate the values of the SOAP header. mySoapHeader.Username = UserName mySoapHeader.Password = SecurelyStoredPassword
' Create a new instance of the proxy class.
Dim proxy As MyWebService = New MyWebService()
' Add the MyHeader SOAP header to the SOAP request. proxy.MyHeaderValue = mySoapHeader
' Call the method on proxy class that communicates with
' your Web service method.
Dim results as String = proxy.MyWebMethod()
' Display the results of the method in a label.
ReturnValue.Text = results
End Sub
</script>
<%@ 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 = SecurelyStoredPassword;
// 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 Web service method.
string results = proxy.MyWebMethod();
// Display the results of the method in a label.
ReturnValue.Text = results;
}
</script>
See Also
Reference
SoapHeader
SoapUnknownHeader
SoapHeaderException
Concepts
Building XML Web Service Clients