mardi 30 avril 2019

Get & Set Request Header Key values using Reflection C#

Using Reflection, I am trying to fetch the request headers of .NET Core web app. I could access the Scheme, Method, etc from the Request object as in the below code,

reqObj.GetType().GetProperty("Scheme").GetValue(reqObj, null).ToString();

I couldnt get the headers of the request object(The class linked HttpRequest ). I am using the lines below that tries to get the Headers and I need to get the keyvalue pairs in it.

Object headerObj = reqObj.GetType().GetProperty("Headers").GetValue(reqObj,null);

var items = headerObj.GetType().GetProperty("Keys", BindingFlags.Instance | BindingFlags.Public).GetValue(headerObj,null);

Here I should get the ICollection of strings. But on that line the process is getting crashed. Even i tried to typecast the headerObj to ICollection<KeyValuePair<String,StringValues>> . But trying those things too gives me a crash.

Here I need to print the header keys and values using Reflection.. Also i m trying to add a key value pair in the header using Reflection. How can i achieve these?





Aucun commentaire:

Enregistrer un commentaire