Chalalo Land

Tecnologías ASP.NET y un poco Más

Contacto


 Si quierer cooperar, yo feliz, muy agradecido :)

De donde me Visitan?

Locations of visitors to this page

Generic Content

Si te gustaron los articulos, y te animas te estaría muy agradecido!


Recent Posts

Tags

Community

Blogs de MVP

Amigos Geeks

Blogs Imperdibles

GODS

Archives

Email Notifications

.[Tips] Conectarse a un Servicio Web ColdFusion desde ASP.NET

Hola , que tal? Este post es un tip,  no es un tutorial, pero va servir, hace poco tuve que conectarme desde ASP.NET a un Servicio Web que estaba construido con ColdFusión, y el clásico y cómodo método de Agregar Referencia Web no es compatible, lo que debemos hacer es utilizar la clases HTTPRequest

Existe poca documentación y ejemplos sobre el tema. Fue mi Amigo y Maestro Mauricio Gómez que me ayudó a resolver el problema.
Es importante contar con el WSDL para obtener el SOAP Envolpe, para eso puedes ocupar un cliente Soap Genérico

http://www.soapclient.com/soaptest.html

Con esto vamos a obtener el SOAP Env, y hacemos una función para crear el “envoltorio SOAP” que envolverá nuestro mensaje:

Private Function GetSoapString2() As String

        Dim soapRequest As New StringBuilder("")

        soapRequest.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:apachesoap=""http://xml.apache.org/xml-soap"" xmlns:impl=""http://ws"" xmlns:intf=""http://ws"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:tns1=""http://rpc.xml.coldfusion"" xmlns:wsdl=""http://schemas.xmlsoap.org/wsdl/"" xmlns:wsdlsoap=""http://schemas.xmlsoap.org/wsdl/soap/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">")

        soapRequest.Append("<SOAP-ENV:Body>")

        soapRequest.Append("<mns:WebMethod xmlns:mns=""http://ws"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">")

        soapRequest.Append("<xmlSet xsi:type=""xsd:string"">{0}</xmlSet>")

        soapRequest.Append("</mns:WebMethod>")

        soapRequest.Append("</SOAP-ENV:Body>")

        soapRequest.Append("</SOAP-ENV:Envelope>")

        Return soapRequest.ToString()

    End Function

 

La clave acá es: soapRequest.Append("<xmlSet xsi:type=""xsd:string"">{0}</xmlSet>"), que nos va a servir al momento de “Envolver”

Public Function CrearLlamadaWS(ByVal ServiceUri As String, ByVal requestXmlString As String) As String

        Dim httpRequest As HttpWebRequest = WebRequest.Create(ServiceUri)

        Dim myWriter As StreamWriter

        Dim result As String = String.Empty

        If Not httpRequest Is Nothing Then

            httpRequest.Method = "POST"

            httpRequest.ContentLength = 1049

            httpRequest.ContentType = "text/xml; charset=""utf-8"""

            httpRequest.Timeout = 60000

            httpRequest.Headers.Add("SOAPAction", "")

            myWriter = New StreamWriter(httpRequest.GetRequestStream())

            Dim xx As String = String.Format(Me.GetSoapString2(), requestXmlString)

            myWriter.Write(xx)

            If Not myWriter Is Nothing Then

                myWriter.Close()

            End If

            Dim objResponse As HttpWebResponse

            Dim esr As StreamReader

 

            Try

                objResponse = httpRequest.GetResponse()

            Catch ex As WebException 'Exception

                esr = New StreamReader(ex.Response.GetResponseStream())

                result = esr.ReadToEnd()

            Catch ex As Exception

                objResponse = Nothing

                Throw

            End Try

 

            Dim sr As StreamReader

            sr = New StreamReader(objResponse.GetResponseStream())

            result = sr.ReadToEnd()

            sr.Close()

 

            objResponse.Close()

            sr.Close()

            objResponse = Nothing

            sr = Nothing

            httpRequest.Abort()

        End If

 

        httpRequest = Nothing

        myWriter = Nothing

        Return result

    End Function

Luego hacemos la llamada:

Dim request As String = "<![CDATA[<misdatos></misdatos>]]>"

MakeWebServiceCall(“http://10.0.0.1/ws/test.cfc?method=WebMethod”, request)

En la mayoria de los casos, el argumento, aunque nos indican que es un XML, en realidad es “en formato XML” pero no un XML propiamente tal, si no que un String:

        soapRequest.Append("<xmlSet xsi:type=""xsd:string"">{0}</xmlSet>")

La llamada es entonces, la hacemos con la URL, que incluye en nombre el WebMethod, y luego el xml con CDATA ya que en realidad es un String.

Vuelvo a dar las gracias a Mauricio Gómez, por ayudarme y por querer compartir esta información Sonrisa

Espero que te sirva,
Saludos,
Chalalo

Posted: 27/9/2010 1:25 por Gonzalo Perez | con 3 comment(s)
Archivado en: ,
Comparte este post:

Comentarios

Andrés Iturralde ha opinado:

N O T A B L E !

# September 28, 2010 8:14 PM

DNL ha opinado:

Excelente nada mas que decir !!!!!!!!1

# September 30, 2010 7:19 AM

Chalalo Land ha opinado:

Ya se acaba el año, y quería hacer un resumen, no están todos los artículos, si no los que más rescato

# December 31, 2010 2:15 AM