You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ofbiz.apache.org by Vinay Agarwal <Vi...@hotmail.com> on 2007/04/25 03:44:29 UTC

Using OFBiz SOAP with .NET 2.0

Hello,

 

After spending way too much time trying to fix WSDL generated code (in .NET)
to access OFBiz SOAP services, I ended up writing a rather simple low-level
"driver" for it. The driver takes input map of parameters (Dictionary in
NET) and returns a map from the output of the service. It currently only
handles strings but can be easily extended as needed.

 

The code is provided below. The main function shows usage of this driver for
running "ping" service on Si's demo server.

 

Regards,

Vinay Agarwal

 

----- Code place in a .cs file and use VS2005 to run

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Text;

using System.Web;

using System.Xml;

 

namespace Soap {

 

    class Program {

        public static string server =
"http://demo.opensourcestrategies.com:8085/webtools/control/SOAPService/";

        public static string UrlNS = "http://www.ofbiz.org/service/";

        public static string UrlEnv =
"http://schemas.xmlsoap.org/soap/envelope/";

        public static string UrlXsi =
"http://www.w3.org/2001/XMLSchema-instance";

        public static string TypString = "xsd:string";

        public static string ResultKey = "responseMessage";

        public static string ResultVal = "success";

        static Dictionary<string, string> SoapService(string command,
Dictionary<string, string> map) {

            byte[] postDataBytes = getPostData(command, map);

            string postData = Encoding.UTF8.GetString(postDataBytes);

            Console.WriteLine(postData);

            HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(server);

            request.Method = "POST";

            request.ContentLength = postDataBytes.Length;

            using (Stream requestStream = request.GetRequestStream()) {

                requestStream.Write(postDataBytes, 0, postDataBytes.Length);

            }

 

            // get response and write to console

            HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

            Dictionary<string, string> result =
getResponse(response.GetResponseStream());

            response.Close();

            return result;

        }

 

        static byte[] getPostData(string command, Dictionary<string, string>
map) {

            using (MemoryStream stream = new MemoryStream()) {

                XmlTextWriter writer = new XmlTextWriter(stream,
Encoding.UTF8);

                writer.WriteStartDocument();

                writer.WriteStartElement("soapenv:Envelope");

                writer.WriteAttributeString("xmlns:soapenv", UrlEnv);

                writer.WriteAttributeString("xmlns:xsi", UrlXsi);

                writer.WriteStartElement("soapenv:Body");

                writer.WriteStartElement(command, UrlNS);

                foreach (KeyValuePair<string, string> entry in map) {

                    writer.WriteStartElement(entry.Key, UrlNS);

                    writer.WriteAttributeString("type", TypString);

                    writer.WriteString(entry.Value);

                    writer.WriteEndElement();

                }

                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.Flush();

                return stream.ToArray();

            }

        }

 

        static Dictionary<string, string> getResponse(Stream stream) {

            Dictionary<string, string> map = new Dictionary<string,
string>();

            try {

                XmlTextReader reader = new XmlTextReader(stream);

                reader.Read(); // move past container

                reader.ReadStartElement("soapenv:Envelope");

                reader.ReadStartElement("soapenv:Body");

                reader.ReadStartElement();

                reader.Read();

                while (reader.NodeType != XmlNodeType.EndElement) {

                    string key = reader.Name;

                    string value = reader.ReadElementString();

                    reader.MoveToContent();

                    map.Add(key, value);

                }

            } catch {

                return null;

            }

            // Check if service call was success or failure

            if (map.ContainsKey(ResultKey)) {

                if (map[ResultKey] == ResultVal) {

                    map.Remove(ResultKey);

                    return map;

                }

            }

            return null;

        }

 

        static void Main(string[] args) {

            Dictionary<string, string> map = new Dictionary<string,
string>();

            map.Add("message", "silly");

            Dictionary<string, string> result = SoapService("ping", map);

            Console.ReadKey();

        }

    }

}