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

发布于 2024-08-05 21:31:13 字数 2128 浏览 5 评论 0原文

我正在尝试使用流量网络服务。下面给出了 SOAP 请求的示例。

我使用 WSDL 结构中的 Wsdl.exe 在 C# 中创建了一个代理类。

我认为我现在需要做的是以某种方式将“验证”SOAP 标头插入到 SOAP 结构中 方法调用。我不确定如何将标头添加到服务方法调用中?

<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>

以下是代理类中用于调用 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]));
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋心╮凉 2024-08-12 21:31:13

在网上搜索时,我发现了一篇关于非常相似的问题和一个很好的解决方案的论坛帖子。
此处可用 - forums.asp.net/t/1137408.aspx

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

耀眼的星火 2024-08-12 21:31:13

与之前 Visual Studio .Net 2003/2005 中的“添加 Web 服务引用”和创建 SOAP 扩展相比,添加 SOAP 标头是 WCF 中更加复杂的事情之一。

要在 WCF 中执行此操作,您需要添加 EndPointBehavior。有很多例子,谷歌搜索 IEndpointBehavior 和 IClientMessageInspector。这个 article 提供了一个很好的简洁示例,但您可能需要扩展它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文