简体   繁体   中英

Adding a custom SOAP header using c#/ASP.NET

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/">
  <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]));
        }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM