I've created a Xamarin.Forms application and added a connected Odata Service. Now when i try sending a request to the backend it will fail with the exception System.ArgumentException: The 'User-Agent' header must be modified using the appropriate property or method. Parameter name: name.
What i've tried:
1: Handling the DataServiceContext_BuildingRequest and setting the header there using e.Headers["User-Agent"] = "xyz";
2: Setting the Content-Type header because i read in another thread somewhere that it helped (e.Headers.Add("Content-Type", "application/json"))
3: Setting the delegate of the DataServiceContext.Configurations.RequestPipeline.OnMessageCreating. The problem is the exception will be thrown as soon as i try to create a new instance of HttpWebRequestMessage passing in the args that it receives.
Pipeline delegate:
Configurations.RequestPipeline.OnMessageCreating = (args) =>
{
args.Headers["User-Agent"] = "xyz";
args.Headers.Add("Content-Type", "application/json");
args.Headers["Accept"] = "application/json";
var req = new HttpWebRequestMessage(args);
req.HttpWebRequest.ContentType = "application/json";
return req;
};
My suspicion regarding this is, that there was something changed in the underlying code that is used by the Odata HttpWebRequestMessage class which doesnt allow direct setting of the User-Agent header anymore.
Did anyone find a way around this?