Exploring the Header in the WSE 2.0 SoapEnvelope Class - Code for the Public Class
(Page 3 of 4 )
As noted previously, here is the code:
Imports System Imports System.Web.Services.Protocols Namespace
SoapHeader Public Class jhdr Inherits
System.Web.Services.Protocols.SoapHeader Public UserName As
String End Class End Namespace
The code for the SopHdr.asmx is shown here:
Imports System.Web.Services.Protocols
Imports Microsoft.Web.Services2
Namespace SoapHeader
<System.Web.Services.WebService(Namespace:="http://tempuri.org/Hdrs/
SopHdr")> _
Public Class SopHdr
Inherits System.Web.Services.Protocols.SoapHeader
Public myHeader As New jhdr
#Region " Web Services Designer Generated Code "
<WebMethod(), SoapHeader("myHeader")> _
Public Function TestHdr() As String
Dim sen1 As New Microsoft.Web.Services2.SoapEnvelope
myHeader.UserName = "mysorian"
sen1.SetBodyObject("OK")
sen1.InnerXml = "<soap:Envelope
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Header/>
<soap:Body>
</soap:Body>
</soap:Envelope>"
sen1.Header.InnerXml = myHeader.UserName
Try
'Return sen1.Header.IsEmpty
Return sen1.Header.OuterXml
Catch ex As SoapHeaderException
Return ex.Message
End Try
End Function
End Class
End Namespace
The following items are to be noted:
WSE2.0 is properly referenced by the imports statement. The statement regarding the inheritance from the System.Web.Services.protocols is included. An instance of the class jhdr is included. Also the SoapHeader reference to the jhdr class is referenced in the Web Method.
With the above in place, a Public function is scripted to get the SoapEnvelope's Header information using the above code. First of all a reference to the SoapEnvelope from WSE2.0 is instantiated with sen1. The SoapEnvelope should have a Body element; hence, a dummy body ["OK"] is set using the SetBodyObject method. Also the root element must be present; this is supplied by the SoapEnvelope's InnerXML setting.
Look at the complete string of the SoapEnvelope. You will observe that an empty SoapHeader element is added. With this preliminary preparation, the method can take all this information and return the SoapEnvelope's header from the web service. The commented return should produce a false since the header is not empty. Errors in the SoapHeader can be thrown as SoapErrorException. For example, if you were to drop the <Soap:Header/> element in the SoapEnvelope string an exception would be thrown.
Next: Running the Code >>
More Windows Scripting Articles
More By Jayaram Krishnaswamy