public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentEncoding]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentLanguage]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentLength == -1 ? string.Empty : request.ContentLength.ToString());
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // If x-ms-date header exists, Date should be empty string
            string dateValue = CommonUtils.GetSingleValueOrDefault(request.Headers.GetValues(Constants.HeaderConstants.Date));

            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = request.Headers[HttpRequestHeader.Date];
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfModifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfNoneMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfUnmodifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Range]);

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request.Headers, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.Address, accountName));

            return(canonicalizedString.Value);
        }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));

                long contentLength = request.Content.Headers.ContentLength.HasValue ? request.Content.Headers.ContentLength.Value : -1;
                canonicalizedString.AppendCanonicalizedElement(contentLength == -1 ? string.Empty : contentLength.ToString());

                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? string.Empty :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? string.Empty :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                if ((request.Method == HttpMethod.Put) ||
                    (request.Method == HttpMethod.Delete))
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(string.Empty);
                }

                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
            }

            // If x-ms-date header exists, Date should be empty string
            string dateValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);

            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = CommonUtils.GetDateTimeValueOrDefault(request.Headers.Date);
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? string.Empty :
                                                           CommonUtils.GetSingleValueOrDefault(request.Headers.Range.Ranges));

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.RequestUri, accountName));

            return(canonicalizedString.Value);
        }