internal static string ExecuteAuthenticatedLyncAutodiscoverRedirect(string requestUrl, ICredentials credentials, int redirectCount)
        {
            string result = string.Empty;

            if (redirectCount >= 10)
            {
                return(result);
            }
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);

            httpWebRequest.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
            try
            {
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK && LyncAutodiscoverWorker.GetUcwaUrlFromHttpWebResponse(httpWebResponse, credentials, redirectCount, out result))
                {
                    return(result);
                }
            }
            catch (WebException ex)
            {
                result = ((ex.InnerException == null) ? ex.Message : ex.InnerException.Message);
            }
            return(result);
        }
        internal static LyncAutodiscoverResult GetUcwaUrl(string authenticatedEndpointUrl, ICredentials credentials)
        {
            LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrl");
            LyncAutodiscoverResult lyncAutodiscoverResult = new LyncAutodiscoverResult();

            lyncAutodiscoverResult.IsUcwaSupported  = false;
            lyncAutodiscoverResult.UcwaDiscoveryUrl = string.Empty;
            string text = string.Empty;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(LyncAutodiscoverWorker.CertificateValidationCallBack);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(authenticatedEndpointUrl);
                httpWebRequest.Accept      = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                httpWebRequest.Timeout     = 120000;
                httpWebRequest.Credentials = credentials;
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState.Request = httpWebRequest;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse.ToString();
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine("Response is NULL");
                }
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrlFromHttpWebResponse to get ucwa URL.");
                    if (LyncAutodiscoverWorker.GetUcwaUrlFromHttpWebResponse(httpWebResponse, credentials, 0, out text))
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("ucwa URL: {0}", text));
                        lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
                        return(lyncAutodiscoverResult);
                    }
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("unable to find ucwa URL from response.", new object[0]));
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Http status code: {0}", httpWebResponse.StatusCode));
                }
            }
            catch (WebException ex)
            {
                LyncAutodiscoverWorker.details.AppendLine(string.Format("Exception: {0}", ex.ToString()));
                HttpWebResponse httpWebResponse2 = ex.Response as HttpWebResponse;
                if (httpWebResponse2 != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse2.ToString();
                    HttpStatusCode statusCode = httpWebResponse2.StatusCode;
                    if (statusCode == HttpStatusCode.Unauthorized)
                    {
                        text = ((ex.InnerException == null) ? ex.Message : ex.InnerException.Message);
                    }
                }
            }
            lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
            lyncAutodiscoverResult.IsUcwaSupported  = !string.IsNullOrWhiteSpace(text);
            lyncAutodiscoverResult.DiagnosticInfo   = LyncAutodiscoverWorker.details.ToString();
            LyncAutodiscoverWorker.details.Clear();
            return(lyncAutodiscoverResult);
        }