2012-06-03 37 views
3

我是一个完整的C#新手,一直在努力,现在没有成功破解这对于几个小时......添加自定义SoapClient的头

我需要建立一个SoapClient的对C#使用...我一直在尝试将现有的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> 

我用Visual Studio的“添加服务引用...”当我通过

服务工作做好,因为使用PHP它出色的作品。

的C#我已经concieved:

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

} 

很明显,我不知道如何实现SOAP头,所以它抛出一个“缺少SOAP头”异常(这是从WebService收到)。

回答

4

我有同样的问题。 我找不到解决方案。最终,我不得不创建整个SOAP消息并通过HTTPWebRequest发送它。

看一看here

-1

下面是关于如何鉴别头发送到肥皂web服务的例子:Authentication for Web Services (using SOAP headers)

+0

如果我得到你的答案,有必要修改服务实现,不是吗?如果您无法使用这些服务,该怎么办? –

+0

@ADC你的意思是服务器端?没有这个例子只是描述了如何创建一个肥皂呼叫(客户端)。 – Anas

+0

我不同意。建议的解决方案指出:“为了强制使用我们的新SOAP头,我们需要在我们的方法中添加以下属性:[...]”。所以看起来该服务必须进行修改。 –

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