// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Net; using System.Xml; namespace HandCoded { public class T1 { public T1() : this(0, "") { } public T1(int f1, string f2) { this.f1 = f1; this.f2 = f2; } public int f1 { get; set; } public string f2 { get; set; } } public class Program { private static XmlDocument Call(string url, Tuple[] hdrs, XmlDocument msg) { string xmlin = msg.OuterXml; WebClient wc = new WebClient(); foreach(Tuple hdr in hdrs) { wc.Headers.Add(hdr.Item1, hdr.Item2); } string xmlout = wc.UploadString(url, xmlin); wc.Dispose(); XmlDocument res = new XmlDocument(); res.LoadXml(xmlout); return res; } private static XmlDocument GetEnvelope() { XmlDocument req = new XmlDocument(); req.AppendChild(req.CreateElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")); req.DocumentElement.AppendChild(req.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")); return req; } private static Tuple[] GetHeaders(string action, string actionprefix, bool useaction) { if(useaction) { return new Tuple[] { Tuple.Create("Content-Type", "text/xml"), Tuple.Create("SOAPAction", "http://soap.ws/" + actionprefix + action) }; } else { return new Tuple[] { Tuple.Create("Content-Type", "text/xml") }; } } public static void Test(string url, string actionprefix, string paramns, string fldns, bool useaction, bool usenames) { XmlDocument req; XmlElement operation; XmlDocument resp; // getOne req = GetEnvelope(); operation = req.CreateElement("getOne", "http://soap.ws"); XmlElement f1getone = req.CreateElement(usenames ? "f1" : "arg0", paramns); f1getone.AppendChild(req.CreateTextNode(123.ToString())); operation.AppendChild(f1getone); req.DocumentElement.LastChild.AppendChild(operation); resp = Call(url, GetHeaders("getOne", actionprefix, useaction), req); XmlElement xo = (XmlElement)resp.DocumentElement.LastChild.FirstChild.FirstChild; XmlNamespaceManager nsmgetone = new XmlNamespaceManager(resp.NameTable); nsmgetone.AddNamespace("tns", fldns); T1 o = new T1(int.Parse(xo.SelectSingleNode("tns:f1/text()", nsmgetone).Value), xo.SelectSingleNode("tns:f2/text()", nsmgetone).Value); Console.WriteLine(o.f1 + " " + o.f2); // getAll req = GetEnvelope(); operation = req.CreateElement("getAll", "http://soap.ws"); req.DocumentElement.LastChild.AppendChild(operation); resp = Call(url, GetHeaders("getAll", actionprefix, useaction), req); XmlElement xall = (XmlElement)resp.DocumentElement.LastChild.FirstChild.FirstChild; XmlNamespaceManager nsmgetall = new XmlNamespaceManager(resp.NameTable); nsmgetall.AddNamespace("tns", fldns); foreach(XmlElement xone in xall.ChildNodes) { T1 one = new T1(int.Parse(xone.SelectSingleNode("tns:f1/text()", nsmgetall).Value), xone.SelectSingleNode("tns:f2/text()", nsmgetall).Value); Console.WriteLine(one.f1 + " " + one.f2); } // save req = GetEnvelope(); operation = req.CreateElement("save", "http://soap.ws"); XmlElement f1save = req.CreateElement(usenames ? "f1" : "arg0", paramns); f1save.AppendChild(req.CreateTextNode(123.ToString())); operation.AppendChild(f1save); XmlElement f2save = req.CreateElement(usenames ? "f2" : "arg1", paramns); f2save.AppendChild(req.CreateTextNode("ABC")); operation.AppendChild(f2save); req.DocumentElement.LastChild.AppendChild(operation); resp = Call(url, GetHeaders("save", actionprefix, useaction), req); XmlElement xressave = (XmlElement)resp.DocumentElement.LastChild.FirstChild.FirstChild; bool ressave = bool.Parse(xressave.FirstChild.Value); Console.WriteLine(ressave); // saveObject req = GetEnvelope(); operation = req.CreateElement("saveObject", "http://soap.ws"); XmlElement osaveobj = req.CreateElement(usenames ? "o" : "arg0", paramns); operation.AppendChild(osaveobj); XmlElement f1saveobj = req.CreateElement("f1", fldns); f1saveobj.AppendChild(req.CreateTextNode(123.ToString())); osaveobj.AppendChild(f1saveobj); XmlElement f2saveobj = req.CreateElement("f2", fldns); f2saveobj.AppendChild(req.CreateTextNode("ABC")); osaveobj.AppendChild(f2saveobj); req.DocumentElement.LastChild.AppendChild(operation); resp = Call(url, GetHeaders("saveObject", actionprefix, useaction), req); XmlElement xressaveobj = (XmlElement)resp.DocumentElement.LastChild.FirstChild.FirstChild; bool ressaveobj = bool.Parse(xressaveobj.FirstChild.Value); Console.WriteLine(ressaveobj); // delete req = GetEnvelope(); operation = req.CreateElement("delete", "http://soap.ws"); XmlElement f1delete = req.CreateElement(usenames ? "f1" : "arg0", paramns); f1delete.AppendChild(req.CreateTextNode(123.ToString())); operation.AppendChild(f1delete); req.DocumentElement.LastChild.AppendChild(operation); resp = Call(url, GetHeaders("delete", actionprefix, useaction), req); XmlElement xresdelete = (XmlElement)resp.DocumentElement.LastChild.FirstChild.FirstChild; bool resdelete = bool.Parse(xresdelete.FirstChild.Value); Console.WriteLine(resdelete); } public static void TestWithHandler(string url, string actionprefix, string paramns, string fldns, bool useaction, bool usenames) { try { Test(url, actionprefix, paramns, fldns, useaction, usenames); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static void Main(string[] args) { TestWithHandler("http://localhost:8080/axis/services/TestService", "", "http://soap.ws", "", true, false); TestWithHandler("http://localhost:8080/wssoap/Test", "", "", "", false, false); TestWithHandler("http://localhost/TestService.asmx", "", "http://soap.ws", "http://soap.ws", true, true); TestWithHandler("http://localhost/Test.svc", "Test/", "http://soap.ws", "http://soap.ws", true, true); TestWithHandler("http://localhost/TestAlt.svc", "Test/", "", "http://soap.ws", true, true); } } }