locked
How to add a SOAP header to a SOAP meesage?

    Question

  • Hi

    From a VB.NET client i call a (non .NET) webservice using a given WSDL file.

    To this message i want to add an Header element with a certain value.

    I got some example code in JAVA, but do not know hwer and how to inplement,

                    // Construct the header
                    QName headerName = new QName("
    http://ech.client.nl/vendor", "Vendor");
                    SOAPHeaderElement headerElement = new SOAPHeaderElement(headerName);
                    headerElement.setValue("ECH Gui");
                    headerElement.setMustUnderstand(true);
                    
                    try {
                            Message message = arg0.getCurrentMessage();
                            message.getSOAPHeader().addChildElement(headerElement);
                    } catch

    The headerelement must have the namespace http://ech.cleint.nl/vendor. The name of the element must be Vendor.

    How do i do this??

     

    Peter

    Wednesday, November 08, 2006 11:37 AM

Answers

  • I got it working. It was just some typo error.

    Thx

    Tuesday, December 12, 2006 10:29 AM

All replies

  • Ok... you should have created the WS proxy using the wsdl, right?

    Then.... create the header class like this:

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://ech.cleint.nl/vendor")]

    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://ech.cleint.nl/vendor", IsNullable=false)]

    public partial class Vendor : System.Web.Services.Protocols.SoapHeader {

         private System.Xml.XmlAttribute[] anyAttrField;

         [System.Xml.Serialization.XmlAnyAttributeAttribute()]

         public System.Xml.XmlAttribute[] AnyAttr {

              get {

                  return this.anyAttrField;

              }

              set {

                  this.anyAttrField = value;

              }

          }

    // ADD PROPERTIES HERE WITH THE INFORMATION YOU MUST PROVIDE

    }

     

    This class must be located in some place so it is accessible in the proxy class.

    Then in your proxy class add a property for your header:

    private Vendor _vendorHeader;

    public Vendor VendorHeader {

         get {

             return this._vendorHeader;

         }

         set {

            this._vendorHeader = value;

         }

    }

    Finally....

    Mark each of the methods that calls the WS webmethods with this attribute:

    [System.Web.Services.Protocols.SoapHeaderAttribute("VendorHeader")]

     

    Finally, to use it.... you just create an instance of your proxy class and set the VendorHeader property with the information you must provide and that's it.

    Rgds

    Rodrigo

    Wednesday, November 08, 2006 11:56 AM
  • Thx for the quick answer Rodrigo.

    I will go through your code (Need to convert it to VB but thats no problem).

    If i have additional question i will come back :)

    Peter

     

    Wednesday, November 08, 2006 12:05 PM
  • Well i think i have it all implemented but i got an error when the proxy class is used for the first time.

    It has to do with the first line. I added that line to the function in the proxy class where i need to send the new SOAP header with the message. What i do wrong?

    Or do i mark the function on the wrong place. (this is the function in the proxy class itself).

     

    <System.Web.Services.Protocols.SoapHeader("VendorHeader")> _ <System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace:="http://berichten.communicatie.ech.bevalue.com", ResponseNamespace:="https://echstag.ilcampo.com/axis/services/ECHBerichtenServices")> _

    Public Function zetAflosOpdracht(ByVal in0 As AflosOpdracht) As <System.Xml.Serialization.SoapElementAttribute("zetAflosOpdrachtReturn")> Acknowledgement

    Dim results() As Object = Me.Invoke("zetAflosOpdracht", New Object() {in0})

    Return CType(results(0), Acknowledgement)

    End Function

    Thursday, November 09, 2006 8:14 AM
  • Peter,

    Could you show me the error you're getting?

    Thx, Rodrigo

    Thursday, November 09, 2006 10:50 AM
  • Rodrigo

    The exception it throws is a TypeInitializationException with an inner exception of 'Method ECHBerichtenServicesService.zetAflosOpdracht can not be shown'. That is the function where i added the header attribute as shown in the code.

    If you want me to lisst all the code i added let me know.

     

    Thursday, November 09, 2006 11:32 AM
  • Peter, I tried that and it worked fine for me....

    Please show me the whole code to see if I can find something wrong there.

    Regards

    Rodrigo

    Thursday, November 09, 2006 8:24 PM
  • I made a new  class:

    <System.Serializable()> _

    <System.Diagnostics.DebuggerStepThrough()> _

    <System.ComponentModel.DesignerCategory("code")> _

    <System.Xml.Serialization.XmlType(Namespace:="http://ech.client.nl/vendor")> _

    <System.Xml.Serialization.XmlRoot(Namespace:="http://ech.client.nl/vendor", IsNullable:=False)> _

    Partial Public Class Vendor : Inherits SoapHeader

    Private anyAttrField As System.Xml.XmlAttribute

    Property AnyAttr() As System.Xml.XmlAttribute

    Get

    Return Me.anyAttrField

    End Get

    Set(ByVal value As System.Xml.XmlAttribute)

    Me.anyAttrField = value

    End Set

    End Property

    'Public sVendor As String

    End Class

    Added this to the proxy class

    Private _Vendorheader As Vendor

    Public Property VendorHeader() As Vendor

    Get

    Return Me._Vendorheader

    End Get

    Set(ByVal value As Vendor)

    Me._Vendorheader = value

    End Set

    End Property

    Added the line to the fucntion that i need to have the header included.

    This function is also in the proxy class

    <System.Web.Services.Protocols.SoapHeader("VendorHeader"), _

    <System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace:="http://berichten.communicatie.ech.bevalue.com", ResponseNamespace:="https://echstag.ilcampo.com/axis/services/ECHBerichtenServices")> _

    Public Function zetAflosOpdracht(ByVal in0 As AflosOpdracht) As <System.Xml.Serialization.SoapElementAttribute("zetAflosOpdrachtReturn")> Acknowledgement

    Dim results() As Object = Me.Invoke("zetAflosOpdracht", New Object() {in0})

    Return CType(results(0), Acknowledgement)

    End Function

    And in the code i set the properties

    funcECH.VendorHeader.MustUnderstand = True

    funcECH.VendorHeader.AnyAttr.Value = "KVBA"

    Monday, November 13, 2006 9:09 AM
  • The actual error was "Method ECHBerichtenServicesService.zetAflosOpdracht can not be reflected."

    I did tanslate it wrong i see.

    Tuesday, November 14, 2006 9:30 AM
  • I got it working. It was just some typo error.

    Thx

    Tuesday, December 12, 2006 10:29 AM
  • hi all ;

    i would like to do same thing by .net..

     

    OMFactory abstractFactory = OMAbstractFactory.getOMFactory();

      OMNamespace namespace = abstractFactory.createOMNamespace(
        "http://AccountSEI.service.sai.my.com/types", "ns3");

      OMElement element = abstractFactory.createOMElement("LOGIN_ID",
        namespace);
      OMElement element1 = abstractFactory.createOMElement("LOGIN_PASSWORD",
        namespace);
      OMElement element2 = abstractFactory.createOMElement("LOGIN_TOKEN",
        namespace);

      element.setText("myid@local.com");
      element1.setText("welcome");

      stub._getServiceClient().addHeader(element);
      stub._getServiceClient().addHeader(element1);

     

     

    so how can i by .net

    Friday, December 15, 2006 12:09 PM
  • Ok... you should have created the WS proxy using the wsdl, right?

    Then.... create the header class like this:

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://ech.cleint.nl/vendor")]

    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://ech.cleint.nl/vendor", IsNullable=false)]

    public partial class Vendor : System.Web.Services.Protocols.SoapHeader {

     

     

         private System.Xml.XmlAttribute[] anyAttrField;

     

         [System.Xml.Serialization.XmlAnyAttributeAttribute()]

         public System.Xml.XmlAttribute[] AnyAttr {

              get {

                  return this.anyAttrField;

              }

              set {

                  this.anyAttrField = value;

              }

          }

    // ADD PROPERTIES HERE WITH THE INFORMATION YOU MUST PROVIDE

    }

     

    This class must be located in some place so it is accessible in the proxy class.

    Then in your proxy class add a property for your header:

    private Vendor _vendorHeader;

    public Vendor VendorHeader {

         get {

             return this._vendorHeader;

         }

         set {

            this._vendorHeader = value;

         }

    }

    Finally....

    Mark each of the methods that calls the WS webmethods with this attribute:

    [System.Web.Services.Protocols.SoapHeaderAttribute("VendorHeader")]

     

    Finally, to use it.... you just create an instance of your proxy class and set the VendorHeader property with the information you must provide and that's it.

    Rgds

    Rodrigo

    Hi,

    How must i do to put these tags in the soapheader?

    <soapenv:Header>                                                                                                                                                                                                                           
          <credentials xmlns:teu-sec-crd="http://site.com/security/credentials/1.0">
             <username>user</username>
             <password>123</password>
          </credentials>                                                                                                                                                                                                                         
     </soapenv:Header> 


    Thank you
    • Edited by Vagueante Friday, August 21, 2009 3:52 PM
    Friday, August 21, 2009 2:21 PM
  • Please tell me why you replied to this thread.  Do you feel it is better to reply to a thread rather than start a new one? Are you unsure whether you should reply to an existing thread, as opposed to starting a new thread?


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Friday, August 21, 2009 2:48 PM
    Moderator