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;
		}
        internal bool PerformSsoCheck(out Uri ssoRedirectUri, IDictionary <string, string> headers = null)
        {
            using (var webClient = GetWebClient(headers, isSsoCheck: true))
            {
                SsoWebClient ssoWebClient = webClient as SsoWebClient;
                if (ssoWebClient != null)
                {
                    try
                    {
                        if (ssoWebClient.CheckIfRedirect(connectionInfo.Server, connectionInfo.UserName))
                        {
                            string spacerChar = "?";
                            if (ssoWebClient.RedirectTo.Contains("?"))
                            {
                                spacerChar = "&";
                            }

                            string portInfo = String.Empty;
                            if (connectionInfo.Port > 0)
                            {
                                portInfo = String.Format(":{0}", connectionInfo.Port);
                            }

                            ssoRedirectUri = new Uri(String.Format("{0}{1}TargetResource={2}://{3}{4}/slm/j_sso_security_check?noRedirect=true",
                                                                   ssoWebClient.RedirectTo, spacerChar, connectionInfo.Server.Scheme, connectionInfo.Server.Host, portInfo));

                            return(true);
                        }
                    }
                    catch (WebException we)
                    {
                        if ((we.Response != null) &&
                            (((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.MethodNotAllowed))
                        {
                            ssoRedirectUri = null;
                            return(false);
                        }

                        throw;
                    }
                }
                else
                {
                    throw new InvalidOperationException("GetWebClient failed to create a SsoWebClient");
                }
            }

            ssoRedirectUri = null;
            return(false);
        }
        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);
        }