Call web service with soap message

15/07/2014 14:14

 public static string CallAsrWebService(string soapMessage, string webServiceUrl, string webServiceLogin, string webServicePassword)
        {
            string soapResult = string.Empty;

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceUrl);
            req.ContentType = "text/xml;";
            req.Method = "POST";
            req.Credentials = new NetworkCredential(webServiceLogin, webServicePassword);

            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soapMessage);
                }
            }

            WebResponse response = req.GetResponse();
            Stream responseStream = response.GetResponseStream();

            Byte[] myData = ReadFully(responseStream);
            soapResult = System.Text.ASCIIEncoding.ASCII.GetString(myData);

            return soapResult;
        }


        public static string GetResponseFromSoapResult(string soapResult, string resultNodeXpath)
        {
            string result = "No result node found.";
            XmlDocument document = new XmlDocument();
            document.LoadXml(soapResult);  //loading soap message as string
            XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);

            manager.AddNamespace("soapenv", "https://schemas.xmlsoap.org/soap/envelope/");
            manager.AddNamespace("soap-env", "https://schemas.xmlsoap.org/soap/envelope/");
            manager.AddNamespace("ns", "https://docs.oasis-open.org/ns/cmis/messaging/200908/");

            XmlNode resultNode = document.SelectSingleNode(resultNodeXpath, manager);
            if (resultNode != null)
            {
                result = resultNode.InnerText;
            }
            return result;
        }

        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }