This project has moved. For the latest updates, please go here.
2

Resolved

NullReferenceExcpetion when Request.UserAgent is NULL

description

The GetRequiredHeaders() method within FiftyOne.Foundation.Mobile.Detection.WebProvider (version 4) throws an exception when the user-agent header is null (e.g. if making a HttpWebRequest in C#)

There are no null reference checks for the following line of code:
var escapedUA = Uri.UnescapeDataString(request.UserAgent).Replace('+', ' ');
The following implementation fixes the issue:
 private static NameValueCollection GetRequiredHeaders(HttpRequest request)
        {
            var headers = request.Headers;
            try
            {

                if (!String.IsNullOrEmpty(request.UserAgent))
                {
                    var escapedUA = Uri.UnescapeDataString(request.UserAgent).Replace('+', ' ');
                    if (escapedUA != request.UserAgent)
                    {
                        headers = new NameValueCollection(request.Headers.Count, request.Headers);
                        if (headers[Constants.UserAgentHeader] != null)
                        {
                            headers[Constants.UserAgentHeader] = escapedUA;
                        }
                    }
                    else
                    {
                        headers = request.Headers;
                    }
                }
            }
            catch (Exception)
            {
            }
            return headers;
        }

comments

weases wrote Nov 10, 2014 at 8:31 AM

iftikhar51 wrote Jun 11, 2015 at 6:55 PM

Marking as resolved, please follow link above to our website to get further details.