Web Service Development Guide : PART IV Extending your Applications : Chapter 15 SOAP and Transport Headers : SOAP Headers Example
SOAP Headers Example
HydraExpress ships with the example <installdir>\examples\webservices\Headerillustrating how to add and retrieve SOAP headers to a message. In this directory you will find a WSDL file, headers.wsdl, a server implementation, HeadersImp.cpp, and a client implementation, HeadersClient.cpp.
Client-Side Code
The client implementation of the invoke_addHeaders() method, which calls the addHeaders() service operation method in the proxy, adds a SOAP header to a rwsf::CallInfo object before sending the request message.
 
void invoke_addHeaders(HeadersProxy& proxy)
{
int in1_in = 15;
int return_ret;
try {
rwsf::CallInfo info; //1
info.addRequestSoapHeader(rwsf::XmlName("MyHeader",
rwsf::XmlNamespace("headers",
"http://www.roguewave.com/examples/webservice/headers")),
"MyValue"); //2
return_ret = proxy.addHeaders(info, in1_in); //3
...
}
//1 Instantiates a rwsf::CallInfo object.
//2 Adds a request SOAP header element named MyHeader, in a namespace with the prefix headers, whose value is the literal string "MyValue".
//3 Calls the addHeaders() method in the proxy, passing the rwsf::CallInfo object containing the header data.
Server-Side Code
On the server side, the implementation HeadersImp.cpp retrieves the header data and adds a new header to the response message whose data is a modified version of the data in the request message header.
 
int HeadersImp::addHeaders(rwsf::CallInfo& info, int in1_in, )
{
std::string headerValue = //1
info.getRequestSoapHeaderValue(rwsf::XmlName("MyHeader",
rwsf::XmlNamespace("headers",
"http://www.roguewave.com/examples/webservice/headers")));
 
info.addResponseSoapHeader(rwsf::XmlName("ServerHeader", //2
rwsf::XmlNamespace("headers",
"http://www.roguewave.com/examples/webservice/headers")),
headerValue + ": From the server");
 
return in1_in;
}
//1 Retrieves the header data into the variable headerValue.
//2 Adds a response SOAP header element named ServerHeader, in a namespace with the prefix headers, whose value is the request header data with the literal string ": From the server" appended.
Viewing the Results
Generate code, build the example (making sure to copy the provided client and server implementations to the app\client and app\server directories respectively), and deploy the server. If you need help, see the procedure in Chapter 3, “Creating a Web Service.”
NOTE >> The provided implementations assume you have generated code using the default -STL option. If you use the -sourcepro option, the provided implementations will fail to compile.
From your command prompt, navigate to your HeadersExample\bin directory where the client executable has been placed.
NOTE >> HydraExpress’s generated client sample implementations all point to the <project-directory>\conf directory to locate the client configuration files. If you move the client executable without maintaining the same code generation directory structure, or you invoke the client from another location, be aware that you must edit the client implementation to maintain the correct path to the conf directory.
Run the example by entering:
Windows
prompt> HeadersClient.exe
Linux
prompt> HeadersClient
The output shows the header data as returned from the server:
 
headerValue = MyValue: From the server
The output is generated by these lines in the client implementation:
 
std::string headerValue =
info.getResponseSoapHeaderValue(rwsf::XmlName("ServerHeader",
rwsf::XmlNamespace("headers",
"http://www.roguewave.com/examples/webservice/headers")));
std::cout << "headerValue = " << headerValue << std::endl;
To see the SOAP header as added to the request message by the client and as added to the response message by the server implementation, look at the log file log.out in the directory where you ran the example. The log shows the request SOAP message:
 
RWSF:*** REQUEST BODY ***
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<headers:MyHeader
xmlns:headers="http://www.roguewave.com/examples/webservice/headers"
env:mustUnderstand="0">MyValue</headers:MyHeader>
</env:Header>
<env:Body>
<rw:addHeaders env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:rw="http://xml.roguewave.com/webservice/">
<in1 xsi:type="xsd:int">15</in1>
</rw:addHeaders>
</env:Body>
</env:Envelope>
RWSF Info: got reply, extracting...
Here is the response:
 
RWSF Info:*** RESPONSE BODY ***
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<headers:ServerHeader
xmlns:headers="http://www.roguewave.com/examples/webservice/headers"
env:mustUnderstand="0">MyValue: From the server</headers:ServerHeader>
</env:Header>
<env:Body>
<ns1:addHeadersResponse
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="http://xml.roguewave.com/webservice/">
<return xsi:type="xsd:int">15</return>
</ns1:addHeadersResponse>
</env:Body>
</env:Envelope>