Forum Xamarin.Forms

HttpWebRequest.UserAgent missing from Xamarin Studio on Mac

MakaniMakani USMember

The following code snippet fails to compile in Xamarin Studio on Mac:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = "CustomUserAgent";

It gives me the following error:
Error CS1061: Type System.Net.HttpWebRequest' does not contain a definition forUserAgent' and no extension method UserAgent' of typeSystem.Net.HttpWebRequest' could be found. Are you missing an assembly reference? (CS1061)

I obviouslyi do have the HttpWebRequest reference because if I remove the UserAgent line everything compiles. What's fascinating, is that when debugging, in the immediate mode when I do request followed by a dot, Intellisense DOES show me UserAgent there, but not when I'm in the editor. To work around the issue in the code I'm having to do the following reflection crap:

var typeInfo = request.GetType().GetTypeInfo(); var propertyInfo = typeInfo.DeclaredProperties.FirstOrDefault(propInfo => propInfo.Name == "UserAgent"); if (propertyInfo != null) { propertyInfo.SetValue(request, useragent); }

I really need to set the UserAgent and HttpClient is not low level enough for my needs (like it does not allow me to have a StringContent with a custom content type but with no encoding; it forces an encoding, but the service I am using fails because of it.

Can you guys please fix the editor/compiler to recognize the UserAgent property?

Sign In or Register to comment.