使用 c#/ASP.NET 添加自定义 SOAP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1458405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a custom SOAP header using c#/ASP.NET
提问by James Hull
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. I'm unsure how to add the header to the service method call?
我认为我现在需要以某种方式将“身份验证”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>
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]));
}
采纳答案by James Hull
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
回答by sipwiz
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. There are quite a few examples around, google on IEndpointBehavior and IClientMessageInspector. This articleprovides a nice succinct example but you may need to expand it.
要在 WCF 中执行此操作,您需要添加一个 EndPointBehavior。周围有很多例子,谷歌上 IEndpointBehavior 和 IClientMessageInspector。该文章提供了一个很好的例子简洁,但你可能需要将其展开。