使用 c#/ASP.NET 添加自定义 SOAP 标头
我正在尝试使用流量网络服务。下面给出了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在网上搜索时,我发现了一篇关于非常相似的问题和一个很好的解决方案的论坛帖子。
此处可用 - 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
与之前 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.