views:

2181

answers:

2

I am trying to use a traffic web service. An example of the SOAP request is given below.

I have created a proxy class in c# using Wsdl.exe from the WSDL structure.

What I think I need to do now in somehow insert the 'authenticate' SOAP header into the SOAP structure for the method call. I'm unsure how to add the header to the service method call?

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.inteleacst.com.au/wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  <SOAP-ENV:Header>
    <ns1:authenticate>
      <SOAP-ENC:Struct>
        <username>username</username>
        <password>password</password>
      </SOAP-ENC:Struct>
    </ns1:authenticate>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:getAllTraffic>
      <States SOAP-ENC:arrayType="xsd:string[3]" xsi:type="ns1:State_Arr">
        <item xsi:type="xsd:string">VIC</item>
        <item xsi:type="xsd:string">NSW</item>
        <item xsi:type="xsd:string">NT</item>
      </States>
      <EventCodes SOAP-ENC:arrayType="xsd:int[1]" xsi:type="ns1:EventCode_arr">
        <item xsi:type="xsd:int">802</item>
      </EventCodes>
    </ns1:getAllTraffic>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here is the code in the proxy class for calling the web service method.

[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://webservice.intelecast.com.au/traffic/PublicSoap/server.php#getAllTraffic", RequestNamespace="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php", ResponseNamespace="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php")]
        [return: System.Xml.Serialization.SoapElementAttribute("return")]
        public TrafficInfo[] getAllTraffic(string[] States, int[] EventCodes) {
            object[] results = this.Invoke("getAllTraffic", new object[] {
                        States,
                        EventCodes});
            return ((TrafficInfo[])(results[0]));
        }
+2  A: 

Adding SOAP headers is one of those things that got more convoluted with WCF compared to the previous "Add Web Service Reference" in Visual Studio .Net 2003/2005 and creating a SOAP extension.

To do it in WCF you need to add an EndPointBehavior. There are quite a few examples around, google on IEndpointBehavior and IClientMessageInspector. This article provides a nice succinct example but you may need to expand it.

sipwiz
I'm not using WCF.
James Hull
The you should use a SoapExtension http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension(VS.71).aspx.
sipwiz
Thanks, I've successfully used a SoapExtension to overcome the header issue.
James Hull
+1  A: 

Searching the web I found a forum post about a very similar problem and a good solution. Available here - forums.asp.net/t/1137408.aspx

James Hull