lunes, 12 de noviembre de 2012

Leer valores enviados por los metodos Get y Post, guardarlos en un archivo de texto, configurado en el web.config

Este es el código fuente:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Configuration; namespace getPostGetMethods { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(ConfigurationManager.AppSettings["path"], true); 
//en esta linea tomamos el path donde queremos que se guarde el archivo *.txt, del web.config. 
//El parametro true habilita el append if (Request.QueryString.Count > 0) 
//comprobamos que se hayan enviado variables por el metodo Get { sw.WriteLine("vars sent " + DateTime.Now + " Get Method");
 //ponemos una cabecera para saber las variables enviadas, con fecha y hora for (int i = 0; i < Request.QueryString.Count; i++) { sw.WriteLine("Index: " + Request.QueryString.GetKey(i) + " Values: " + Request.QueryString[i]); 
//escribimos todos los indices y valores de todas las variables } sw.WriteLine(" "); } if (Request.Form.Count > 0)//lo mismo que lo anterior pero para el metodo post { sw.WriteLine("vars sent " + DateTime.Now + " Post Method"); for (int i = 0; i < Request.Form.Count; i++) { sw.WriteLine("Index: " + Request.Form.GetKey(i) + " Values: " + Request.Form[i]); } sw.WriteLine(" "); } sw.Close();//cerramos el archivo } } }

Al web.config le agregamos el siguiente XML:

  <appSettings>
    <add key="path" value="c:\\prueba.txt" />
  </appSettings>



No hay comentarios:

Publicar un comentario