2014-06-11 245 views
0

我有這段代碼試圖發送一個請求到肥皂服務器,我是新來肥皂,所以我不知道如果我使用的術語是否正確,請糾正我,我錯了。SOAP服務請求

基本上我通過SOAP請求

這裏訪問名爲getUserDomain Web服務方法是代碼:

public void CallWebService() 
{ 
    var _url = "https://....com/QcXmlWebService/QcXmlWebService.asmx?wsdl"; 
    var _action = "https://....com/QcXmlWebService/QcXmlWebService.asmx?op=GetUserDomains"; 

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope(); 
    HttpWebRequest webRequest = CreateWebRequest(_url, _action); 
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); 
    webRequest.BeginGetResponse(null, null); 
    // begin async call to web request. 
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); 

    // suspend this thread until call is complete. You might want to 
    // do something usefull here like update your UI. 
    asyncResult.AsyncWaitHandle.WaitOne(); 

    // get the response from the completed web request. 
    string soapResult; 
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) 
    { 
     using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) 
     { 
      soapResult = rd.ReadToEnd(); 
     } 
     Console.Write(soapResult); 
    } 
} 

private HttpWebRequest CreateWebRequest(string url, string action) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
    webRequest.Headers.Add("SOAPAction", action); 
    webRequest.ContentType = "text/xml;charset=\"utf-8\""; 
    webRequest.Accept = "text/xml"; 
    webRequest.Method = "POST"; 
    return webRequest; 
} 

private XmlDocument CreateSoapEnvelope() 
{ 
    XmlDocument soapEnvelop = new XmlDocument(); 
    string oRequest = ""; 
    oRequest = @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:qcx=""http://smething.com/QCXML"">"; 
    oRequest = oRequest + "<soap:Header/>"; 
    oRequest = oRequest + "<soap:Body>"; 
    oRequest = oRequest + "<qcx:GetUserDomains>"; 
    oRequest = oRequest + "<qcx:inputXml><![CDATA["; 
    oRequest = oRequest + "<GetUserDomains>"; 
    oRequest = oRequest + "<login>"; 
    oRequest = oRequest + "<domain_name>MBB_BTS</domain_name>"; 
    oRequest = oRequest + "<project_name>WCDMA_BTS_IV</project_name>"; 
    oRequest = oRequest + "<user_name>user</user_name>"; 
    oRequest = oRequest + "<password>pass</password>"; 
    oRequest = oRequest + "</login>"; 
    oRequest = oRequest + "</GetUserDomains>"; 
    oRequest = oRequest + " ]]>"; 
    oRequest = oRequest + "</qcx:inputXml>"; 
    oRequest = oRequest + "</qcx:GetUserDomains>"; 
    oRequest = oRequest + "</soap:Body>"; 
    oRequest = oRequest + "</soap:Envelope>"; 
    soapEnvelop.LoadXml(oRequest); 
    return soapEnvelop; 
} 

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

此代碼我已經在堆棧溢出某處見過作爲一個答案,但我不能」噸得到它的工作......我得到的錯誤是拋出異常System.net.webexception。遠程服務器返回錯誤:(500)內部服務器

+1

不要通過字符串連接構建XML。是否有理由不只是使用「添加服務參考」? –

回答

1

遵循以下步驟:

1)點擊項目 - >添加服務引用 2)點擊在底部的「高級」按鈕「添加服務參考「窗口 3)單擊」服務參考設置「窗口底部的」添加Web參考「按鈕 4)在URL欄中輸入您的URL以訪問服務。

不需要你編碼所有的東西。一旦服務添加到您的項目中,您可以聲明它的一個實例並根據需要使用它。