在 C# 中为 Web 服务调用添加自定义 SOAPHeader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18980689/
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 custom SOAPHeader in C# for a web service call
提问by user2810857
I am trying to add a custom soap header information in c# before calling a web service. I am using SOAP Header class to get this done. I could do this partly but not completely the way I need it. Here is how I need the soap header to look like
我正在尝试在调用 Web 服务之前在 C# 中添加一个自定义的soap 头信息。我正在使用 SOAP Header 类来完成这项工作。我可以部分地但不能完全按照我需要的方式做到这一点。这是我需要肥皂头的样子
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>USERID</Username>
<Password>PASSWORD</Password>
</UsernameToken>
</Security>
</soap:Header>
<soap:Body>
...
I am able to add soap header as below
我可以添加如下所示的肥皂头
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Username>UserID</Username>
<Password>Test</Password>
</UsernameToken>
</soap:Header>
<soap:Body>
What I am not able to do is add the "Security" elements which wraps the "UsernameToken" as in the first sample. Any help would be appreciated.
我无法做的是添加包装“UsernameToken”的“Security”元素,就像在第一个示例中一样。任何帮助,将不胜感激。
回答by PBMe_HikeIt
This link adding soap headerworked for me. I am calling a SOAP 1.1 service that I did not write and have no control over. Am using VS 2012 and added the service as a web reference in my project. Hope this helps
这个添加肥皂头的链接对我有用。我正在调用一个 SOAP 1.1 服务,该服务不是我编写的,也无法控制。我正在使用 VS 2012 并在我的项目中添加了该服务作为 Web 参考。希望这可以帮助
I followed the steps 1-5 from J. Dudgeon's post towards the bottom of the thread.
我按照 J. Dudgeon 帖子中的步骤 1-5 走到了线程的底部。
Here's some sample code (this would be in a separate .cs file):
这是一些示例代码(这将在单独的 .cs 文件中):
namespace SAME_NAMESPACE_AS_PROXY_CLASS
{
// This is needed since the web service must have the username and pwd passed in a custom SOAP header, apparently
public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol
{
public Creds credHeader; // will hold the creds that are passed in the SOAP Header
}
[XmlRoot(Namespace = "http://cnn.com/xy")] // your service's namespace goes in quotes
public class Creds : SoapHeader
{
public string Username;
public string Password;
}
}
And then in the generated proxy class, on the method that calls the service, per J Dudgeon's step 4 add this attribute: [SoapHeader("credHeader", Direction = SoapHeaderDirection.In)]
然后在生成的代理类中,在调用服务的方法上,按照 J Dudgeon 的第 4 步添加此属性: [SoapHeader("credHeader", Direction = SoapHeaderDirection.In)]
finally, here's the call to the generated proxy method, with the header:
最后,这是对生成的代理方法的调用,带有标题:
using (MyService client = new MyService())
{
client.credHeader = new Creds();
client.credHeader.Username = "username";
client.credHeader.Password = "pwd";
rResponse = client.MyProxyMethodHere();
}