Entrada destacada

Solucion error AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts

Ajax Control Toolkit en ASP.NET Ajax Library Detalles de la Versión :(AspNetAjaxLibraryBeta0911.zip application, 6490K, uploaded Nov 18 ...

jueves, 18 de diciembre de 2008

Consumir Web Services de Indicadores con ASP

En una publicación anterior, vimos como podíamos conectarnos a un Web Services utilizando el lenguaje de programación Visual Basic 6. Hoy veremos como conectarnos al mismo Web Services que nos entregaba los indicadores económicos de Chile. Empecemos.

Comencemos por crear el formulario que recibirá la fecha de consulta e invocará al Web Services que se llamará “RescateWS.asp”. El código es el siguiente:




<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>http://turboprogramacion.blogspot.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.Estilo1 {
color: #FFFFFF;
font-weight: bold;
font-size: 18px
}
-->
</style>
</head>
<body>
<form action="Invocar.asp" method="post" name="form1">
<table width="300" border="0" cellspacing="0" cellpadding="0" align="center">
<tr bgcolor="#004080">
<td colspan="3"><div align="center" class="Estilo1">Consutar Indicadores Econ&oacute;micos</div></td>
</tr>
<tr bgcolor="#CCCCCC">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr bgcolor="#CCCCCC">
<td width="61%" align="right">Ingrese Fecha (yyyymmdd)</td>
<td width="4%">:</td>
<td width="35%"><input name="fecha" type="text" id="fecha" size="10" maxlength="10"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr bgcolor="#808080">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input name="Consultar" type="submit" value="Consultar"></td>
</tr>
</table>
</form>
</body>
</html>



Ahora el archivo “Funcion.asp”, en donde vamos a crear la función “LeeXml()” que se encargará de leer el XML de respuesta con los indicadores que nos enviará el Web Services y la función “InvocarWebService()” que enviará el XML de consulta con el parámetro fecha. Nuestras funciones quedarán así:




<%
Function LeeXml(responseText)
Dim xmlResponse
Dim xnodelist
Dim indicadores
Dim id, uf, usd, euro, utm, tcm, fecha

indicadores = ""
If Len(responseText) <> 0 Then
Set xmlResponse = CreateObject("MSXML2.DOMDocument")
xmlResponse.async = false
xmlResponse.loadXml responseText
Set xnodelist = xmlResponse.documentElement.selectNodes("/soap:Envelope/soap:Body/IndicadoresResponse/IndicadoresResult/diffgr:diffgram/NewDataSet/indicadores")
Dim objItem

For Each objItem In xnodelist
id = objItem.selectSingleNode("id").Text
uf = objItem.selectSingleNode("uf").Text
usd = objItem.selectSingleNode("usd").Text
euro = objItem.selectSingleNode("euro").Text
utm = objItem.selectSingleNode("utm").Text
tcm = objItem.selectSingleNode("tcm").Text
fecha = objItem.selectSingleNode("fecha").Text
Next

indicadores = "<TABLE CELLSPACING='1' bgcolor='#dedede' ALIGN=CENTER WIDTH='250'>" & _
"<TR bgcolor='#f0f0f0'>" & _
"<TD>UF</TD>" & _
"<TD align='right'>"&uf&"</TD>" & _
"</TR>" & _
"<TR bgcolor='#f0f0f0'>" & _
"<TD>UTM</TD>" & _
"<TD align='right'>"&utm&"</TD>" & _
"</TR>" & _
"<TR bgcolor='#f0f0f0'>" & _
"<TD>D&oacute;lar Observado</TD>" & _
"<TD align='right'>"&usd&"</TD>" & _
"</TR>" & _
"<TR bgcolor='#f0f0f0'>" & _
"<TD>Euro</a></TD>" & _
"<TD align='right'>"&euro&"</TD>" & _
"</TR>" & _
"</TABLE>"
End If

LeeXml = indicadores
End Function

Function InvocarWebService (strSoap, strSOAPAction, strURL, ByRef xmlResponse)
Dim xmlhttp
Dim blnSuccess

Set xmlhttp = server.CreateObject("WinHttp.WinHttpRequest.5.1")
xmlhttp.Open "POST", strURL
xmlhttp.setRequestHeader "Man", "POST " & strURL & " HTTP/1.1"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", strSOAPAction
call xmlhttp.send(strSoap)

If xmlhttp.Status = 200 Then
blnSuccess = True
Else
blnSuccess = False
End If

xmlResponse = xmlhttp.ResponseText
InvocarWebService = blnSuccess
Set xmlhttp = Nothing
End Function
%>



Ya solo nos queda crear la página “Invocar.asp” que contiene el XML de consulta y llama a las funciones “InvocarWebService()” y “LeeXml()” para posteriormente presentarnos los indicadores resultantes por pantalla. Entonces el código es:


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!-- #include file = "funcion.asp" -->
<%
strFecha = Request.Form("fecha")
strSoap = "<?xml version='1.0' encoding='utf-8'?>"& _
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"& _
"<soap:Body>"& _
"<Indicadores xmlns='Indicadores'>"& _
"<Fecha>"&strFecha&"</Fecha>"& _
"</Indicadores>"& _
"</soap:Body>"& _
"</soap:Envelope>"
strSOAPAction = "Indicadores/Indicadores"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>http://turboprogramacion.blogspot.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
Dim xmlResponse
If InvocarWebService (strSoap, strSOAPAction, "http://www.desachile.com/webservice.asmx?WSDL", xmlResponse) Then
Response.Write(LeeXml(xmlResponse))
Else
Response.Write "*** Ha ocurrido un Error ***"
End If
Set xmlResponse = Nothing
%>
</body>
</html>



Listo, ya solo nos queda probar. Si estás detrás de un Proxy, puede que tengas que hacer una pequeña variación en el código. Eso se los dejo como tarea.


Saludos y espero les ayude a quienes están comenzando con este tema.


Toby


Los archivos del ejemplo, los puedes descargar en DesaChile.com

No hay comentarios:

Publicar un comentario

Entradas populares