Webservices Windows Mobile

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.


Note.gif Note:

EXENCIÓN DE RESPONSABILIDAD - Esto guia/tip es escrito por --John Agudelo 15:38, 15 February 2014 (UTC), de O.S. Group Colombia. Cualquier aporte es bienvenido para discutir y mejorar.

Introducción

Actualmente muchas empresas en el mercado utilizan dispositivos industriales para algunos de sus procesos como manejo de inventarios, captura de pedidos, entre otros los cuales cuentan con sistema operativo Windows Mobile para hacer la integración con Adempiere_Web_Services es necesario tener instalado visual studio 2005 o visual studio 2008 con Windows Mobile 6.5 Developer Tool Kit .

Implementación

Este es el codigo necesario para consumir el webservices.

   using System;
   using System.Linq;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Text;
   using System.Windows.Forms;
   using System.Xml;
   using System.Net;
   using System.IO;
   using System.Xml.Linq;
   public void CallWebService()
   {
       //define url of webservices
       Console.WriteLine("Sending..");
       var _url = "http://ip_servidor_adempiere:puerto/ADInterface/services/ModelADService";
       var _action = "";
       XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
       HttpWebRequest webRequest = CreateWebRequest(_url, _action);
       InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
       // 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();
           }
           XDocument docResponses = XDocument.Parse(soapResult);
           Console.WriteLine("Response:");
           Console.WriteLine(docResponses.Root.Value);
       }
   }
   private XmlDocument CreateSoapEnvelope()
   {
       XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
       XNamespace adin = "http://3e.pl/ADInterface";
       var XML = new XDocument(
          new XDeclaration("1.0", "utf-8", String.Empty),
           new XElement(soapenv + "Envelope",
               new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
               new XAttribute(XNamespace.Xmlns + "adin", adin),
               new XElement(soapenv + "Header"),
               new XElement(soapenv + "Body",
               new XElement(adin + "createData",
                   new XElement(adin + "ModelCRUDRequest",
                       new XElement(adin + "ModelCRUD",
                           new XElement(adin + "serviceType", "CreateBPartner"),
                           new XElement(adin + "TableName", "C_BPartner"),
                           new XElement(adin + "RecordID", "0"),
                           new XElement(adin + "Action", "Create"),
                       new XElement(adin + "DataRow",
                           new XElement(adin + "field",new XAttribute("column","Value"),
                               new XElement(adin + "val", "900329286-8")),
                           new XElement(adin + "field", new XAttribute("column", "Name"),
                               new XElement(adin + "val", "Open Source Group S.A.S")),
                           new XElement(adin + "field",new XAttribute("column","TaxID"),
                               new XElement(adin + "val", "900329286-8")),
                           new XElement(adin + "field",new XAttribute("column","IsVendor"),
                               new XElement(adin + "val", "Y")),
                           new XElement(adin + "field",new XAttribute("column","IsCustomer"),
                               new XElement(adin + "val", "N")),
                           new XElement(adin + "field",new XAttribute("column","IsTaxExempt"),
                               new XElement(adin + "val", "N")),
                           new XElement(adin + "field",new XAttribute("column","Name2"),
                               new XElement(adin + "val", "http://www.osgroup.co")),
                           new XElement(adin + "field",new XAttribute("column","C_BP_Group_ID"),
                               new XElement(adin + "val", "104"))
                               )//datarow
                           ),//modelcrud
                       new XElement(adin + "ADLoginRequest",
                           new XElement(adin + "user", "WebService"),
                           new XElement(adin + "pass", "WebService"),
                           new XElement(adin + "lang", "en_US"),
                           new XElement(adin + "ClientID", "11"),
                           new XElement(adin + "RoleID", "50004"),
                           new XElement(adin + "OrgID", "11"),
                           new XElement(adin + "WarehouseID", "103"),
                           new XElement(adin + "stage", "9")
                           ) //ADloginrequest
                       ) // crud request
                   )//createdata
                   )//body
                   )//soapenv
               );//doc
       XmlDocument xmldoc = new XmlDocument();
       xmldoc.LoadXml(XML.ToString());
       string xmlcontents = xmldoc.InnerXml;
       xmldoc.LoadXml(xmlcontents);
       return xmldoc;
   }
   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 void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
   {
       if (webRequest is HttpWebRequest)
           ((HttpWebRequest)webRequest).AllowWriteStreamBuffering = true;
       using (Stream stream = webRequest.GetRequestStream())
       {
           soapEnvelopeXml.Save(stream);
       }
   }