0

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?

Toastbrewt
  • 43
  • 4
  • in most cases the userAgent is provided by the environment so Id search for options to set it on Xamarin – jonathan Heindl Mar 29 '19 at 20:14
  • Thank you for the advice. I've checked that out right now but unfortunatly it seems that in xamarin this only applies to webviews and viewing external pages what im not doing/using. – Toastbrewt Mar 30 '19 at 11:05

0 Answers0