How to add a SOAP header to a SOAP meesage?

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 namespacehttp://ech.cleint.nl/vendor. The name of the element must be Vendor.

How do i do this?

Peter

[1632 byte] By [PetervanderVeenNL] at [2007-12-26]
# 1

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

rfreire at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 2

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

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 3

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

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 4

Peter,

Could you show me the error you're getting?

Thx, Rodrigo

rfreire at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 5

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.

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 6

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

rfreire at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 7

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"

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 8

The actual error was "Method ECHBerichtenServicesService.zetAflosOpdracht can not be reflected."

I did tanslate it wrong i see.

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 9

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

Thx

PetervanderVeenNL at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 10

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

radhe at 2007-9-4 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...

.NET Development

Site Classified