Cliente SOAP para servicios PHP o Java AXIS

Abril 14, 2008 :: Posted by - Emilio Torrens :: Category - , , ,

Aquí dejo una clase para consumir servicios web vía SOAP en los que no tengamos el WSDL o que el Wizard nos de problemas:

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace Test
    {
    class SOAPClient
        {

        public static XmlDocument SendMsg(XmlDocument xmlRQ)
            {
                byte[] byte1;
                Stream myStream;
                StreamReader readStream;

                HttpWebRequest wr =
                    (HttpWebRequest)WebRequest.
                    Create("http://laurl.com/ws.php");

                byte1 =
                Encoding.GetEncoding(1252).GetBytes(xmlRQ.OuterXml);
                wr.Method = "POST";
                wr.Headers.Add("SOAPAction", "");
                wr.ContentType = "text/xml; charset=\"utf-8\"";
                wr.Accept = "text/xml";

                myStream = wr.GetRequestStream();
                myStream.Write(byte1, 0, byte1.Length);
                myStream.Close();

                myStream = wr.GetResponse().GetResponseStream();
                readStream = new StreamReader(myStream);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(readStream);
                myStream.Close();

                return getSOAPPayload(xmlDoc, "body");
            }

          private static XmlDocument getSOAPPayload(XmlDocument rs,
                                                    string Tag)
            {

                XmlNode xmlNode =
                    rs.GetElementsByTagName(Tag)[0].FirstChild;
                XmlDocument xmlResult = new XmlDocument();
                xmlResult.LoadXml(xmlNode.Value);
                return xmlResult;
            }

        }
    }

Si te gusta compartelo ...
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DotNetKicks
  • Live
  • MySpace
  • Meneame
  • Twitter

Tags: , , ,

Leave a Reply