private AutoDiscoverResponseXML GetResponse(WebResponse response)
        {
            this.TraceHeaders(response.Headers);
            AutoDiscoverResponseXML result;

            using (Stream responseStream = response.GetResponseStream())
            {
                try
                {
                    SafeXmlSerializer       safeXmlSerializer       = new SafeXmlSerializer(typeof(AutoDiscoverResponseXML));
                    AutoDiscoverResponseXML autoDiscoverResponseXML = safeXmlSerializer.Deserialize(responseStream) as AutoDiscoverResponseXML;
                    if (this.url.StartsWith("http:", StringComparison.OrdinalIgnoreCase))
                    {
                        this.TraceVerbose(Strings.TowsDomainNotSsl(this.url));
                    }
                    result = autoDiscoverResponseXML;
                }
                catch (InvalidOperationException ex)
                {
                    if (ex.InnerException == null || !(ex.InnerException is XmlException))
                    {
                        throw;
                    }
                    result = null;
                }
            }
            return(result);
        }
 public AutoDiscoverResponseXML Invoke()
 {
     return(this.ExecuteAndReportErrors <AutoDiscoverResponseXML>(delegate
     {
         AutoDiscoverResponseXML result = null;
         this.credentialsImpersonator.Impersonate(delegate(ICredentials credentials)
         {
             HttpWebRequest httpWebRequest = this.SendRequest(credentials);
             using (WebResponse response = httpWebRequest.GetResponse())
             {
                 result = this.GetResponse(response);
             }
         });
         return result;
     }));
 }
            public override Exception ValidateResponse(Stream responseStream, out string ewsUrl, out string oabUrl)
            {
                ewsUrl = null;
                oabUrl = null;
                SafeXmlSerializer       safeXmlSerializer       = new SafeXmlSerializer(typeof(AutoDiscoverResponseXML));
                AutoDiscoverResponseXML autoDiscoverResponseXML = null;
                XDocument xdocument = null;

                try
                {
                    autoDiscoverResponseXML = (AutoDiscoverResponseXML)safeXmlSerializer.Deserialize(responseStream);
                    responseStream.Seek(0L, SeekOrigin.Begin);
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        xdocument = XDocument.Load(streamReader);
                    }
                }
                catch (XmlException innerException)
                {
                    return(new ServiceValidatorException(Strings.ErrorInvalidResponseFormat, innerException));
                }
                if (autoDiscoverResponseXML.ErrorResponse != null)
                {
                    if (autoDiscoverResponseXML.ErrorResponse.Error == null)
                    {
                        return(new ServiceValidatorException(Strings.ErrorInvalidResponseXml(xdocument.ToString())));
                    }
                    return(new ServiceValidatorException(Strings.ErrorResponseContainsError(autoDiscoverResponseXML.ErrorResponse.Error.ErrorCode, autoDiscoverResponseXML.ErrorResponse.Error.Message)));
                }
                else
                {
                    if (autoDiscoverResponseXML.DataResponse == null || autoDiscoverResponseXML.DataResponse.Account == null || autoDiscoverResponseXML.DataResponse.Account.Action == null)
                    {
                        return(new ServiceValidatorException(Strings.ErrorInvalidResponseXml(xdocument.ToString())));
                    }
                    if (autoDiscoverResponseXML.DataResponse.Account.Action.Equals("redirectAddr", StringComparison.OrdinalIgnoreCase))
                    {
                        string redirectAddr = autoDiscoverResponseXML.DataResponse.Account.RedirectAddr;
                        return(new ServiceValidatorException(Strings.ErrorAutoDiscoverValidatorRequiresRedirection(redirectAddr)));
                    }
                    if (autoDiscoverResponseXML.DataResponse.Account.Protocol == null)
                    {
                        return(new ServiceValidatorException(Strings.ErrorInvalidResponseXml(xdocument.ToString())));
                    }
                    AutoDiscoverProtocol[] protocol = autoDiscoverResponseXML.DataResponse.Account.Protocol;
                    string[] array = new string[]
                    {
                        "EXCH",
                        "EXPR"
                    };
                    for (int i = 0; i < array.Length; i++)
                    {
                        string protocolType = array[i];
                        AutoDiscoverProtocol autoDiscoverProtocol = Array.Find <AutoDiscoverProtocol>(protocol, (AutoDiscoverProtocol p) => p.Type.Equals(protocolType, StringComparison.OrdinalIgnoreCase));
                        if (autoDiscoverProtocol != null)
                        {
                            if (!string.IsNullOrEmpty(autoDiscoverProtocol.EwsUrl))
                            {
                                ewsUrl = autoDiscoverProtocol.EwsUrl;
                            }
                            if (!string.IsNullOrEmpty(autoDiscoverProtocol.OABUrl))
                            {
                                oabUrl = autoDiscoverProtocol.OABUrl;
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(ewsUrl))
                    {
                        return(new ServiceValidatorException(Strings.ErrorAutoDiscoverValidatorEwsNotFound(xdocument.ToString())));
                    }
                    if (string.IsNullOrEmpty(oabUrl))
                    {
                        return(new ServiceValidatorException(Strings.ErrorAutoDiscoverValidatorOabNotFound(xdocument.ToString())));
                    }
                    return(null);
                }
                Exception result;

                return(result);
            }