WebClient GetWebClient(IEnumerable<KeyValuePair<string, string>> headers = null, bool isCacheable = false, bool isSsoCheck = false)
		{
			CookieAwareWebClient webClient;
			if (isCacheable)
				webClient = new CookieAwareCacheableWebClient(cookies);
			else if (isSsoCheck)
				webClient = new SsoWebClient(cookies);
			else
				webClient = new CookieAwareWebClient(cookies);

			if (connectionInfo.AuthType == AuthorizationType.ApiKey)
				webClient.Headers.Add("ZSESSIONID", connectionInfo.ApiKey);
			else if (connectionInfo.AuthType == AuthorizationType.ZSessionID)
				webClient.AddCookie(connectionInfo.Server, "ZSESSIONID", connectionInfo.ZSessionID);

			webClient.Encoding = Encoding.UTF8;
			if (headers != null)
			{
				foreach (var pairs in headers)
					webClient.Headers.Add(pairs.Key, pairs.Value);
			}

			if (credentials != null)
				webClient.Credentials = credentials;
			if (connectionInfo.Proxy != null)
				webClient.Proxy = connectionInfo.Proxy;
			return webClient;
		}
        /// <summary>
        /// Gets a cacheable response.
        /// </summary>
        /// <exception cref="RallyUnavailableException">Rally returned an HTML page. This usually occurs when Rally is off-line. Please check the ErrorMessage property for more information.</exception>
        /// <exception cref="RallyFailedToDeserializeJson">The JSON returned by Rally was not able to be deserialized. Please check the JsonData property for what was returned by Rally.</exception>
        internal DynamicJsonObject GetCacheable(Uri target, out bool isCachedResult, IDictionary <string, string> headers = null)
        {
            DynamicJsonObject response        = null;
            DateTime          startTime       = DateTime.Now;
            String            requestHeaders  = "";
            String            responseHeaders = "";

            try
            {
                using (var webClient = GetWebClient(headers, true))
                {
                    requestHeaders = webClient.Headers.ToString();
                    CookieAwareCacheableWebClient cacheableWeb = webClient as CookieAwareCacheableWebClient;
                    if (cacheableWeb != null)
                    {
                        response = cacheableWeb.DownloadCacheableResult(target, out isCachedResult);
                    }
                    else
                    {
                        throw new InvalidOperationException("GetWebClient failed to create a CookieAwareCacheableWebClient");
                    }

                    responseHeaders = webClient.ResponseHeaders.ToString();
                    return(response);
                }
            }
            finally
            {
                TraceHelper.TraceHttpMessage("GET", startTime, target, requestHeaders, response, responseHeaders);
            }
        }
        WebClient GetWebClient(IEnumerable <KeyValuePair <string, string> > headers = null, bool isCacheable = false, bool isSsoCheck = false)
        {
            CookieAwareWebClient webClient;

            if (isCacheable)
            {
                webClient = new CookieAwareCacheableWebClient(cookies);
            }
            else if (isSsoCheck)
            {
                webClient = new SsoWebClient(cookies);
            }
            else
            {
                webClient = new CookieAwareWebClient(cookies);
            }

            if (connectionInfo.AuthType == AuthorizationType.ApiKey)
            {
                webClient.Headers.Add("ZSESSIONID", connectionInfo.ApiKey);
            }
            else if (connectionInfo.AuthType == AuthorizationType.ZSessionID)
            {
                webClient.AddCookie(connectionInfo.Server, "ZSESSIONID", connectionInfo.ZSessionID);
            }

            webClient.Encoding = Encoding.UTF8;
            if (headers != null)
            {
                foreach (var pairs in headers)
                {
                    webClient.Headers.Add(pairs.Key, pairs.Value);
                }
            }

            if (credentials != null)
            {
                webClient.Credentials = credentials;
            }
            if (connectionInfo.Proxy != null)
            {
                webClient.Proxy = connectionInfo.Proxy;
            }
            return(webClient);
        }
        /// <summary>
        /// Gets a cacheable response.
        /// </summary>
        /// <exception cref="RallyUnavailableException">Rally returned an HTML page. This usually occurs when Rally is off-line. Please check the ErrorMessage property for more information.</exception>
        /// <exception cref="RallyFailedToDeserializeJson">The JSON returned by Rally was not able to be deserialized. Please check the JsonData property for what was returned by Rally.</exception>
        internal DynamicJsonObject GetCacheable(Uri target, out bool isCachedResult, IDictionary <string, string> headers = null)
        {
            DynamicJsonObject response        = null;
            DateTime          startTime       = DateTime.Now;
            String            requestHeaders  = "";
            String            responseHeaders = "";

            try
            {
                using (var webClient = GetWebClient(headers, true))
                {
                    requestHeaders = webClient.Headers.ToString();
                    CookieAwareCacheableWebClient cacheableWeb = webClient as CookieAwareCacheableWebClient;
                    if (cacheableWeb != null)
                    {
                        response = cacheableWeb.DownloadCacheableResult(target, out isCachedResult);
                    }
                    else
                    {
                        throw new InvalidOperationException("GetWebClient failed to create a CookieAwareCacheableWebClient");
                    }

                    responseHeaders = webClient.ResponseHeaders.ToString();
                    return(response);
                }
            }
            finally
            {
                Trace.TraceInformation("Get ({0}):\r\n{1}\r\nRequest Headers:\r\n{2}Response Headers:\r\n{3}Response Data\r\n{4}",
                                       DateTime.Now.Subtract(startTime).ToString(),
                                       target.ToString(),
                                       requestHeaders,
                                       responseHeaders,
                                       response);
            }
        }