Thanks Mykola!

I was looking at the generated code as well and tried it as you
described. Adding headers manually did indeed work, but the downside
being that everytime I regenerate the code it will be gone.

After some tinkering, I figured out a way to have Flex's generator
honor the headers for me! Here's how to do it:

1) You must have the WS side of your app include the header info
directly in its WSDL, such as:

<wsdl:operation name="ListUsers">
<soap:operation soapAction="http://xyz.com/ListUsers"; style="document"/>
  <wsdl:input>
    <soap:body use="literal"/>
    <soap:header message="tns:ListUsersSessionHeader"
part="SessionHeader" use="literal"/>
  </wsdl:input>
  <wsdl:output>
    <soap:body use="literal"/>
  </wsdl:output>
</wsdl:operation>

2) When you re-generate the code in Flex, it will then honor the
<soap:header> tag (along with the type definition for "SessionHeader"
at the beginning of the WSDL).

You will see new method generated on the XYZService class, such as
"addYOURWSMETHODNAME_header(header:YOURHEADERTYPE)". You call this
method to add the header you defined to the corresponding method, so
that every subsequent call will include the header.

(Note that the generated BaseService class actually includes header as
part of the WS call signature, but the generated top-level Service
class does not require header to be passed in the actual WS method
call each time. I think that's a good approach, since usually you use
headers for stuff like Session key which doesn't change often)

3) So how do you include the <soap:header> tag in WSDL? That depends
on your Back End. I'm currently using .Net, so I needed to add a
[SoapHeader] Attribute to the [WebMethods] that I want to include a
header. See MSDN doc here:
http://msdn2.microsoft.com/en-us/library/system.web.services.protocols.soapheader.aspx

Hope this helps!

--- In flexcoders@yahoogroups.com, "Mykola" <[EMAIL PROTECTED]> wrote:
>
> Hi Dave,
> 
> Here is how I did that. I am using Flex Builder 3. I generated code
> from WSDL and updated my operation in the BaseXYZ class. It is just 1
> line change:
> 
> public function hello():AsyncToken
> {
>       // Instead of a new Array, am using the headers from the base class.
>       var headerArray:Array = headers; // <- my change. It was "new
> Array()" here
>             var out:Object = new Object();
>             currentOperation =
>
BaseAddNumbersServiceService.getPort("BaseAddNumbersServicePort").binding.portType.getOperation("hello");
>             var pc:PendingCall = new PendingCall(out,headerArray);
>             call(currentOperation,out,pc.token,pc.headers);
>             return pc.token;
> }
> 
> Now, set the headers and call the service operation:
> 
> // create the service
>       var ws:AddNumbersService = new AddNumbersService();
> 
> // create the headers
>       var q1:QName=new QName("http://soapinterop.org/xsd";, "Header1");
>       var header1:SOAPHeader = new SOAPHeader(q1,
> {string:"bologna",int:"123"});
> 
>       ws.getWebService().addSimpleHeader("Header1",
> "http://soapinterop.org/xsd";, "testName", "testValue");
>       ws.getWebService().addHeader(header1);
> 
> // set the result and error handlers
>       ws.addhelloEventListener(helloResulHandler);
>       ws.addAddNumbersServiceFaultEventListener(faultHandler);
> 
> // call the service
>       ws.hello_send();
> 
> Here is what I see in tcpdump:
> 
> Referer: file://localhost/Users/mykola/Documents/Flex Builder
> 3/SampleClient1/bin-debug/SampleClient1.swf
> Content-type: text/xml; charset=utf-8
> SOAPAction: "urn:hello"
> Content-length: 606
> 
> <SOAP-ENV:Envelope
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";
> xmlns:xsd="http://www.w3.org/2001/XMLSchema";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
>   <SOAP-ENV:Header>
>     <ns0:Header1 xmlns:ns0="http://soapinterop.org/xsd";>
>       <ns0:testName>testValue</ns0:testName>
>     </ns0:Header1>
>     <ns0:Header1 xmlns:ns0="http://soapinterop.org/xsd";>
>       <ns0:int>123</ns0:int>
>       <ns0:string>bologna</ns0:string>
>     </ns0:Header1>
>   </SOAP-ENV:Header>
>   <SOAP-ENV:Body>
>     <ns10:hello xmlns:ns10="http://service.jaxws.mdzyuba.com/"/>
>   </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
> 
> Both headers are sent successfully. I hope it will work for you.
> 
> - Mykola
> Web: http://mdzyuba.googlepages.com
> 
> 
> --- In flexcoders@yahoogroups.com, "Dave Kong" <davekong@> wrote:
> >
> > I have used the code generator in Flex and it was fairly smooth. All
> > the web methods from the WSDL were corrected generated and call/result
> > pairs come in bug-free.
> > 
> > However, I can't figure out how to add some SOAP headers to the
> > service so that every call can contain my headers. Does anyone have
> > experience with this? I really do like the generator and do not want
> > to manually write my service code. (There are too many web methods).
> > 
> > I see a BaseXYZService which extends from AbstractWebService, however,
> > calling its addHeader method succeeds, but no headers were included in
> > the actual SOAP message.  I noticed that the generated code uses
> > WSDLOperation and other WSDLxxx objects, which probably does not honor
> > AbstractWebService.headers array. (I assume those WSDLxxx are used by
> > Apache Axis 2, can anyone point me to the source?)
> > 
> > Any help to add headers to the Flex generated webservice code is
> > GREATLY APPRECIATED!
> >
>


Reply via email to