简体   繁体   English

使用c#/ ASP.NET添加自定义SOAP标头

[英]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. 下面给出了SOAP请求的示例。

I have created a proxy class in c# using Wsdl.exe from the WSDL structure. 我已经从WSDL结构中使用Wsdl.exe在c#中创建了一个代理类。

What I think I need to do now in somehow insert the 'authenticate' SOAP header into the SOAP structure for the method call. 我想我现在需要做的是以某种方式将“身份验证” SOAP头插入方法调用的SOAP结构中。 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. 这是代理类中用于调用Web服务方法的代码。

[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 在此处可用-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. 与以前的Visual Studio .Net 2003/2005中的“添加Web服务参考”和创建SOAP扩展相比,添加SOAP标头是WCF困扰的事情之一。

To do it in WCF you need to add an EndPointBehavior. 要在WCF中执行此操作,您需要添加一个EndPointBehavior。 There are quite a few examples around, google on IEndpointBehavior and IClientMessageInspector. 谷歌上有很多例子,关于IEndpointBehavior和IClientMessageInspector。 This article provides a nice succinct example but you may need to expand it. 本文提供了一个很好的简洁示例,但您可能需要对其进行扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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