2013-10-21 54 views
1

web服務我有一個類:C#發送對象與SOAP

[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] 
[System.Runtime.Serialization.DataContractAttribute(Name="MyClass", Namespace="http://model.common.party.ent.gfdi.be")] 
[System.SerializableAttribute()] 
public class MyClass : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged 
{ 

    [System.Runtime.Serialization.OptionalFieldAttribute()] 
    private string firstname; 

    [System.Runtime.Serialization.OptionalFieldAttribute()] 
    private string lastname; 
} 

我創建一個實例:

var myClass = new MyClass() { lastname = "AAA", firstname = "BBB" }; 

我想這種情況下發送到Web服務。

我想將此對象發送到Web服務。通過Web服務接收到該消息應該是這樣的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:membership="http://service.common.party.ent.gfdi.be 
http://service.common.party.ent.gfdi.be"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <membership:find> 
     <membership:in0> 
      <membership:lastname>AAA</membership:lastname> 
      <membership:firstname>BBB</membership:firstname> 
     </membership:in0> 
     </membership:find> 
    </soapenv:Body> 
</soapenv:Envelope> 

我嘗試這樣做:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(myurl); 
webRequest.Headers["Authorization"] = "Basic " + "XXXXXXXXXXXXXX="; 
webRequest.ContentType = "text/xml; charset=UTF-8"; 
webRequest.Method = "POST"; 

XmlDocument soapEnvelopeXml = CreateSoapEnvelope(); 
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); 

IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); 

asyncResult.AsyncWaitHandle.WaitOne(); 

string soapResult; 
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) 
{ 
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) 
    { 
     soapResult = rd.ReadToEnd(); 
    } 
    Console.Write(soapResult); 
} 

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) 
{ 
    using (Stream stream = webRequest.GetRequestStream()) 
    { 
     soapEnvelopeXml.Save(stream); 
    } 
} 

private static XmlDocument CreateSoapEnvelope() 
{ 
    XmlDocument soapEnvelop = new XmlDocument(); 
    soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>"); 
    return soapEnvelop; 
} 

任何想法?

感謝,

+0

此服務是否有wsdl描述? –

+0

實際嘗試發送您想要發送的消息而不是您目前在代碼中使用的某種Hello World示例如何? –

+0

@CarlosLanderas是的。 「問題」是當我在瀏覽器中複製/粘貼URL時,在訪問WSDL之前詢問登錄名/密碼(通過HTML頁面),但我不知道如何解決問題(我嘗試使用Credential但不工作) –

回答

0

讀你的意見,我會做到以下幾點:

  • 我會請求Web服務URL的用戶名/密碼,並使用Internet Explorer(視覺工作室之外)進行身份驗證。

  • 然後在項目中,使用添加服務引用 - >高級 - >添加web引用。選項,並介紹它不應請求認證的URL,因爲IExplorer已經過認證。

  • 如果你能得到的WSDL服務描述解析和創建的WS類,那麼你做,因爲你可以提供web服務的基本身份驗證,一旦你想用消耗的web服務:

    WebServiceClass wsClass = new WebServiceClass(); 
    wsClass .Credentials = new System.Net.NetworkCredential("user", "password");