Ask the Expert

Working with SOAP and adding header info to messages

I'm very new to Web services so please be gentle! I have a Web service that returns a SOAP envelope to a client thus
 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:tns="http://machine.domain.com"
 xmlns:types="http://machine.domain.com/encodedTypes"
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <tns:SimonsXMLResponse>
       <SimonsXMLResult xsi:type="xsd:string">string</SimonsXMLResult>
     </tns:SimonsXMLResponse>
   </soap:Body>
 </soap:Envelope>
Firstly, how I can change "<soap:Envelope " to "<env:Envelope" ? Also the my webmethod returns a string thus
 return mystr;
And the ASP.NET parser seems to do a wonderful job of encoding this as
 <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <tns:SimonsXMLResponse>
       <SimonsXMLResult xsi:type="xsd:string">string</SimonsXMLResult>
     </tns:SimonsXMLResponse>
   </soap:Body>
How can I make the return into, say for example,
 <env:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <fe:Conversation>Hello</fe:Conversation>
 </env:Body>
Finally, how do I add header information to my messages. The format I'm after is something like...
 <env:Header>
    <fe:MessageHeader>
       <sif:SIF_Name>blahdy blah</sif:SIF_Name>
    </fe:MessageHeader>
 </env:Header>

    Requires Free Membership to View

1- Regarding the env: namespace qualifier:
I have to ask why you would want to do such a thing. The string that you use as your namespace qualifier is irrelevant. (it's basically a variable name--you just have to use it consistently within your message.) The SOAP runtime system that generates the SOAP message (in your case ASP.NET) chooses the namespace qualifier. I don't think you have the ability to tell ASP/NET to use env: rather than soap:. If you really want to do this, you have three choices:
1- use a different SOAP runtime system that uses the env: namespace qualifier
2- use a low-level API (or use a straight XML processing API) to manually construct your SOAP message
3- transform the message when you receive it using XSLT

2- Regarding the response message format:
The ASP.NET-generated response message is generated because you are using RPC/encoded. If you want to return your example message then you would need to define the service as document/literal (but in that case you would not specify the encodingStyle attribute). Instead your return message would look something like:
   <env:Body xmlns:fe="fe-namespace-uri">
       <fe:Conversation>Hello</fe:Conversation>
   </env:Body>
3- Regarding building SOAP headers:
Here are some resources for you:
- a free article with code samples
- a for fee article that explain how to develop SOAP Headers using ASP.NET
- Rob Howard's page (see ASP.NET Advanced Web Services presentation and code samples)

This was first published in June 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: