function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

WSDL2Apex - adding my own SOAP Headers to the apex class

The WSDL I am generating through WSDL2Apex does not include the necessary SOAP Headers. Is there anyway to modify the generated apex code to include the SOAP Header properties? If so, how do I do that? I see the inputHttpHeaders__x but I am looking for the equivalent for SOAP Headers (if they exist)? 



Message Edited by Drew1815 on 08-07-2009 02:48 PM

If the soap header is defined in the wsdl then, wsdl2apex will add a public field for it on the stub.


Thanks for responding.  


Since the WSDL I am importing doesn't have the headers defined in the WSDL itself, can I manually add those public fields to the apex class myself? If so, can you share a simple example to get me started?  



Do you want to send the header or receive the header?


It will be easy to add the header to the wsdl and run it through wsdl2apex.


I want to send soap headers.


I can try to manually add the headers to the WSDL, but any chance you have sample Apex code that shows headers generated as Apex code? 


Here is the wsdl with header:



<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:http="" xmlns:soap="" xmlns:s="" xmlns:soapenc="" xmlns:tns="" targetNamespace="" xmlns:wsdl=""> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace=""> <s:element name="EchoString"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="input" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="EchoStringResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="EchoStringResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="SessionHeader"> <s:complexType> <s:sequence> <s:element name="sessionId" type="s:string"/> </s:sequence> </s:complexType> </s:element> </s:schema> </wsdl:types> <wsdl:message name="EchoStringSoapIn"> <wsdl:part name="parameters" element="tns:EchoString" /> </wsdl:message> <wsdl:message name="EchoStringSoapOut"> <wsdl:part name="parameters" element="tns:EchoStringResponse" /> </wsdl:message> <wsdl:message name="MyHeader"> <wsdl:part name="SessionHeader" element="tns:SessionHeader" /> </wsdl:message> <wsdl:portType name="DocSamplePortType"> <wsdl:operation name="EchoString"> <wsdl:input message="tns:EchoStringSoapIn" /> <wsdl:output message="tns:EchoStringSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="DocSampleBinding" type="tns:DocSamplePortType"> <wsdl:operation name="EchoString"> <soap:operation soapAction="" style="document" /> <wsdl:input> <soap:header use="literal" message="tns:MyHeader" part="SessionHeader" /> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="DocSample"> <wsdl:port name="DocSamplePort" binding="tns:DocSampleBinding"> <soap:address location="" /> </wsdl:port> </wsdl:service> </wsdl:definitions>




Here is the generated stub:



//Generated by wsdl2apex public class demo { public class EchoStringResponse_element { public String EchoStringResult; private String[] EchoStringResult_type_info = new String[]{'EchoStringResult','','string','0','1','false'}; private String[] apex_schema_type_info = new String[]{'','true','false'}; private String[] field_order_type_info = new String[]{'EchoStringResult'}; } public class DocSamplePort { public String endpoint_x = ''; public Map<String,String> inputHttpHeaders_x; public Map<String,String> outputHttpHeaders_x; public String clientCert_x; public String clientCertPasswd_x; public Integer timeout_x; public demo.SessionHeader_element SessionHeader; private String SessionHeader_hns = 'SessionHeader='; private String[] ns_map_type_info = new String[]{'', 'demo'}; public String EchoString(String input) { demo.EchoString_element request_x = new demo.EchoString_element(); demo.EchoStringResponse_element response_x; request_x.input = input; Map<String, demo.EchoStringResponse_element> response_map_x = new Map<String, demo.EchoStringResponse_element>(); response_map_x.put('response_x', response_x); WebServiceCallout.invoke( this, request_x, response_map_x, new String[]{endpoint_x, '', '', 'EchoString', '', 'EchoStringResponse', 'demo.EchoStringResponse_element'} ); response_x = response_map_x.get('response_x'); return response_x.EchoStringResult; } } public class EchoString_element { public String input; private String[] input_type_info = new String[]{'input','','string','0','1','false'}; private String[] apex_schema_type_info = new String[]{'','true','false'}; private String[] field_order_type_info = new String[]{'input'}; } public class SessionHeader_element { public String sessionId; private String[] sessionId_type_info = new String[]{'sessionId','','string','1','1','false'}; private String[] apex_schema_type_info = new String[]{'','true','false'}; private String[] field_order_type_info = new String[]{'sessionId'}; } }





Those samples were very helpful and I am getting closer.


But now I am getting duplicate elements in my SOAP Header element. 

Here is a snippet of my SOAP Header XML:


<AWSAccessKeyId xmlns=""><AWSAccessKeyId>12345</AWSAccessKeyId></AWSAccessKeyId>

 Notice the AWSAccessKeyId is redundant. 



Here is my Apex class with the header element and namespace defined in the stub:


public queueAmazonawsComDoc20090201.AWSAccessKeyId_element AWSAccessKeyId; private String AWSAccessKeyId_hns = 'AWSAccessKeyId=';

Lastly, in my web service method, before I invoke the call, I instantiate the header element, and set the public String property name AWSAccessKeyId:


AWSAccessKeyId = new queueAmazonawsComDoc20090201.AWSAccessKeyId_element(); AWSAccessKeyId.AWSAccessKeyId = key;


All of this compiles and executes the callout but incorrectly since the <AWSAccessKeyId> element is sent twice - one for the namespace and one with the actual string value set. 


Any ideas how to consolidate this into 1 soap header in the XML?   



Is this solved?


Please let me know, I am also looking for SOAP headers. I need to pass issuer name and key for wsdl binding.