Justin Russo
Justin Russo

Reputation: 339

Capturing http header attributes

Hi I have the below code implemented in C# for capturing header attributes. For some reason Im only getting the data for firstname and the remaining fields show up as null. i know they have values but for some reason they are displaying as null.

// Load ServerVariable collection into NameValueCollection object.
            System.Collections.Specialized.NameValueCollection headerdetails = Request.Headers;

            List<string> requiredHeaders = new List<string>();
            requiredHeaders.Add("FirstName");
            requiredHeaders.Add("MiddleName");
            requiredHeaders.Add("LastName");
            requiredHeaders.Add("email");


            // Get names of all keys into a string array. 
            String[] arr1 = headerdetails.AllKeys;
            for (int i = 0; i < arr1.Length; i++)
            {
                if (requiredHeaders.Contains(arr1[i]))
                {

                    if (arr1[i] == "FirstName")
                    {
                        String[] arr2 = headerdetails.GetValues(arr1[i]);
                        for (int values = 0; values < arr2.Length; values++)
                        {
                            string firstname = Server.HtmlEncode(arr2[values]);
                            Session["firstName"] = firstname;
                        }
                    }
                    if (arr1[i] == "MiddleName")
                    {
                        String[] arr2 = headerdetails.GetValues(arr1[i]);
                        for (int values = 0; values < arr2.Length; values++)
                        {
                            string middlename = Server.HtmlEncode(arr2[values]);
                            Session["middleName"] = middlename;
                        }
                    }
                    if (arr1[i] == "LastName")
                    {
                        String[] arr2 = headerdetails.GetValues(arr1[i]);
                        for (int values = 0; values < arr2.Length; values++)
                        {
                            string lastname = Server.HtmlEncode(arr2[values]);
                            Session["lastName"] = lastname;
                        }
                    }
                    if (arr1[i] == "email")
                    {
                        String[] arr2 = headerdetails.GetValues(arr1[i]);
                        for (int values = 0; values < arr2.Length; values++)
                        {
                            string email = Server.HtmlEncode(arr2[values]);
                            Session["emailID"] = email;
                        }
                    }

Upvotes: 0

Views: 325

Answers (1)

Justin Russo
Justin Russo

Reputation: 339

Alrite guys i figured out my mistake. dum one, had a response.redirect within the loop and hence i was only getting the first name. PS: note to self - not to self work long hours.

Upvotes: 1

Related Questions