C# 添加自定义SoapClient头

C# 添加自定义SoapClient头,c#,soap,soap-client,soapheader,C#,Soap,Soap Client,Soapheader,我是个十足的新手,已经尝试破解这个问题好几个小时了,但没有成功 我需要构建一个在C#上使用的SoapClient。。。我一直在尝试将现有的php客户端移植到c# 基本上:我需要使用包含用户和密码的Soap头发出请求,这是我应该发送的xml示例 <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xm

我是个十足的新手,已经尝试破解这个问题好几个小时了,但没有成功

我需要构建一个在C#上使用的SoapClient。。。我一直在尝试将现有的php客户端移植到c#

基本上:我需要使用包含用户和密码的Soap头发出请求,这是我应该发送的xml示例

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.paymentmanager.mycheck.com">
    <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>test</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <SOAP-ENV:Body>
        <ns1:testws>
            <ns1:x>1</ns1:x>
        </ns1:testws>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

显然,我不知道如何实现Soap头,因此它抛出了一个“缺少Soap头”异常(从Web服务接收到)。

下面是一个关于如何向Soap Web服务发送身份验证头的示例:

我也遇到了同样的问题。 我没有找到解决办法。最后,我不得不创建整个SOAP消息,并通过HTTPWebRequest发送它


看一看。

如果我得到你的答案,有必要修改服务实现,不是吗?如果您无法访问这些服务怎么办?您是说服务器端?不,这个例子只描述了如何创建soap调用(客户端)。我不同意。建议的解决方案声明:“为了强制使用新的SOAP头,我们需要向方法添加以下属性:[……]。因此,似乎必须修改服务。我们需要添加soap标头以对SoapClient进行身份验证。@AlbertoDeCaro否,您将
[SoapHeader
注释添加到相应web服务客户端存根中客户端的
[WebMethod
-注释方法中。
namespace ConsoleApplication1
{
    class Program
    {
        const String VENDOR_ID = "8723";
        const String VENDOR_PASS = "test";
        const String VENDOR_USER = "pass";
        static void Main(string[] args)
        {
            try
            {
                PaymentManager.PaymentManagerPortTypeClient test = new PaymentManager.PaymentManagerPortTypeClient();
                int num = test.testws(5);
                Console.WriteLine(num);
            }
            catch( Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

}
        XmlDocument ReqDoc = new XmlDocument();
        XmlDocument doc = new XmlDocument();
            // request message
        ReqDoc.Load(@"D:\104Resqurst.xml");
           //adding soap headder 
        XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("soapenv", "Envelope", "http://www.w3.org/2001/XMLSchema-instance"));
        root.SetAttribute("xmlns", "http://mark.com/services/contracts");
        XmlElement header = (XmlElement)root.AppendChild(doc.CreateElement("soapenv", "Header", "http://www.w3.org/2001/XMLSchema-instance"));
        XmlElement body = (XmlElement)root.AppendChild(doc.CreateElement("soapenv", "Body", "http://www.w3.org/2001/XMLSchema-instance"));
       //assigning the request document to soap header doc
        doc.GetElementsByTagName("soapenv:Body")[0].InnerXml = ReqDoc.OuterXml;