C#: send and recieve SOAP packets as plain text
Mon, 2011-10-10 09:56 | SergeChel
This is a simple example of a way to exchange plain text SOAP messages with XML web-service, without the use of OOP wrappers. This code may be refactored by removing the use of XmlDocument and XmlTextWriter classes.
// prepare outgoing SOAP text XmlDocument doc = new XmlDocument(); doc.LoadXml(soapQuestion); // prepare web request Uri uri = new Uri(soapURI); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Timeout = 100000000; request.Method = "POST"; // you may need these headers //req.Headers.Add("SOAPAction", ""); //req.ContentType = "application/soap+xml;charset=\"utf-16\""; //req.Accept = "application/x-www-form-urlencoded"; //req.Accept = "application/soap+xml"; // do request Stream stream = request.GetRequestStream(); doc.Save(stream); stream.Close(); // get response WebResponse response = request.GetResponse(); stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string result = reader.ReadToEnd(); // process incoming SOAP text doc = new XmlDocument(); doc.LoadXml(result); doc.PreserveWhitespace = true; StringWriter stringwriter = new StringWriter(); XmlTextWriter xmlwriter = new XmlTextWriter(stringwriter); xmlwriter.Formatting = Formatting.Indented; doc.WriteTo(xmlwriter); // result string soapAnswer = stringwriter.ToString();