0

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;
                        }
                    }
2
  • You're requiring these headers from a client accessing your site? Why? These aren't standard headers, what mechanism do you have in place to ensure that a client would send these? For the actual issue at hand, what debugging have you done? Have you ensured that the client is sending these headers in the request? When you step through this code server-side, where does it go wrong? Commented Jul 16, 2013 at 15:12
  • Yes the client is send these headers. I have them being captured in my logs as well. Commented Jul 16, 2013 at 15:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to say, have you checked the casing. You're dependant on the case of the header being exactly as you've defined it. You may want to change your equalities to case-insensitive matching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.