I would like to record client information when they go into a location on the web site so I created this ProcessRequest class that will collect data and insert it into the database. This will collect HttpContext and send that data to a Store Procedure that will then insert into a database table.
namespace DataCollection
{
public class siteDataCollection
{
public void ProcessRequest(HttpContext context)
{
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string filename = Path.GetFileName(context.Request.Url.AbsolutePath);
string customerGuid = context.Request.Cookies["customerguid"].Value.ToString();
customerGuid = customerGuid.Replace("%2D", "-");
if(customerGuid is null) {
customerGuid = HttpContext.Current.Session.SessionID;
}
SQLHelper sqlh = new SQLHelper(conn);
SqlDataReader dr = null;
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter("@CustomerGUID", SqlDbType.NVarChar, 50),
new SqlParameter("@FileName", SqlDbType.NVarChar, 255),
new SqlParameter("@REMOTE_ADDR", SqlDbType.NVarChar,15),
new SqlParameter("@LOCAL_ADDR", SqlDbType.NVarChar,15),
new SqlParameter("@HTTP_USER_AGENT", SqlDbType.NVarChar,255),
new SqlParameter("@HTTP_REFERER", SqlDbType.NVarChar,255)
};
parms[0].Value = customerGuid;
parms[1].Value = filename;
parms[2].Value = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
parms[3].Value = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
parms[4].Value = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
parms[5].Value = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
dr = sqlh.ExecuteReaderStoreProcedure("usp_InsertFileLocation", parms);
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader("Pragma", "no-cache");
context.Response.AddHeader("Expires", "-1");
context.Response.ContentType = "application/pdf";
byte[] bytePDF = File.ReadAllBytes(filename);
context.Response.BinaryWrite(bytePDF);
context.Response.End();
}
}
}