GetHeaders() public method

public GetHeaders ( ) : WebHeaderCollection
return System.Net.WebHeaderCollection
Exemplo n.º 1
0
        const string SCHEMA_NAME_SEPARATOR = " "; //$NON-NLS-1$

        #endregion Fields

        #region Methods

        /// <summary>Handle an authentication failure and possibly return a new response.</summary>
        /// <remarks>Handle an authentication failure and possibly return a new response.</remarks>
        /// <param name="conn">the connection that failed.</param>
        /// <returns>new authentication method to try.</returns>
        internal static HttpAuthMethod ScanResponse(HttpURLConnection conn)
        {
            HttpAuthMethod authentication = NONE;
            var headers = conn.GetHeaders();

            if (headers != null)
            {
                foreach (var authHeader in headers.GetValues(HttpSupport.HDR_WWW_AUTHENTICATE))
                {
                    if (!string.IsNullOrEmpty(authHeader))
                    {
                        var valueParts = authHeader.Split(SCHEMA_NAME_SEPARATOR.ToCharArray(), 2);
                        var method = valueParts[0];
                        var param = valueParts.Length == 1 ? string.Empty : valueParts[1];

                        if (Sharpen.Runtime.EqualsIgnoreCase(HttpAuthMethod.Digest.NAME, method))
                        {
                            return new HttpAuthMethod.Digest(param);
                        }

                        if (Sharpen.Runtime.EqualsIgnoreCase(HttpAuthMethod.Basic.NAME, method))
                        {
                            authentication = new HttpAuthMethod.Basic();
                        }
                    }
                }
            }

            return authentication;
        }