2012-06-03 58 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;