Example #1
0
        public ASCommandResponse GetResponse()
        {
            string StatusCode = string.Empty;
            string Meaning    = string.Empty;
            string Cause      = string.Empty;
            string Resolution = string.Empty;

            EASTester.EasHelp oHelp = new EASTester.EasHelp();

            GenerateXMLPayload();

            //if (ProtocolVersion == null)
            //    throw new InvalidDataException("ASCommandRequest not initialized - Protocol version not specified.");

            //if (ProtocolVersion == null)
            //    throw new InvalidDataException("ASCommandRequest not initialized - EAS Protocol version must be set");

            //if (WbxmlBytes == null)
            //    throw new InvalidDataException("ASCommandRequest not initialized - Request is empty.");

            //if (Server == null)
            //    throw new InvalidDataException("ASCommandRequest not initialized - Server must be specified.");

            //if (Credentials == null && useCertificateAuthentication == false)
            //    throw new InvalidDataException("ASCommandRequest not initialized for authentication.");

            //if (useCertificateAuthentication == true && certificateFile.Length == 0)
            //    throw new InvalidDataException("ASCommandRequest not initialized - Certificate file must be specified.");

            string uriString = string.Format("{0}//{1}/Microsoft-Server-ActiveSync?{2}",
                                             useSSL ? "https:" : "http:", server, RequestLine);
            Uri serverUri = new Uri(uriString);

            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uriString);

            //httpReq.ClientCertificates.
            httpReq.Method      = "POST";
            httpReq.ContentType = "application/vnd.ms-sync.wbxml";

            // Handle any certificate errors on the certificate from the server.
            //ServicePointManager.CertificatePolicy = new CertPolicy();

            //bool bSettingCredsWasOK = false;
            if (useCertificateAuthentication == true)
            {
                // https://support.microsoft.com/en-us/kb/895971
                try
                {
                    httpReq.UseDefaultCredentials = false;
                    X509Certificate oX509Certificate = null;

                    //oX509Certificate = new X509Certificate.CreateFromCertFile(certificateFile);

                    oX509Certificate = new X509Certificate(certificateFile, certificatePassword);
                    httpReq.ClientCertificates.Add(oX509Certificate);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception: \r\n" + ex.ToString(), "Error setting certificate authentication.");
                    return(null);
                }
            }
            else
            {
                try
                {
                    CredentialCache creds = new CredentialCache();
                    creds.Add(serverUri, "Basic", credential);  // Using Basic authentication
                    httpReq.Credentials = creds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception: \r\n" + ex.ToString(), "Error setting specified credentials.");
                    return(null);
                }
            }



            if (SpecifyProxySettings == true)
            {
                WebProxy oWebProxy = null;
                oWebProxy = new WebProxy(ProxyServer, ProxyPort);
                //oWebProxy.BypassProxyOnLocal = BypassProxyForLocalAddress;

                if (OverrideProxyCredntials == true)
                {
                    if (ProxyUser.Trim().Length == 0)
                    {
                        oWebProxy.UseDefaultCredentials = true;
                    }
                    else
                    {
                        if (ProxyDomain.Trim().Length == 0)
                        {
                            oWebProxy.Credentials = new NetworkCredential(ProxyUser, ProxyPassword);
                        }
                        else
                        {
                            oWebProxy.Credentials = new NetworkCredential(ProxyUser, ProxyPassword, ProxyDomain);
                        }
                    }
                }
                else
                {
                    oWebProxy.UseDefaultCredentials = true;
                }

                httpReq.Proxy = oWebProxy;
            }

            if (UserAgent.Trim().Length != 0)
            {
                httpReq.UserAgent = UserAgent;
            }

            if (!UseEncodedRequestLine)
            {
                httpReq.Headers.Add("MS-ASProtocolVersion", ProtocolVersion);
                httpReq.Headers.Add("X-MS-PolicyKey", PolicyKey.ToString());
            }

            try
            {
                Stream requestStream = httpReq.GetRequestStream();
                requestStream.Write(WbxmlBytes, 0, WbxmlBytes.Length);
                requestStream.Close();

                HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

                //float iisStatusCode = (float)httpResp.StatusCode;


                ASCommandResponse response = WrapHttpResponse(httpResp);

                httpResp.Close();

                //StatusCode = iisStatusCode.ToString();
                //Meaning = string.Empty;
                //Cause = string.Empty;
                //Resolution = string.Empty;
                //oHelp.GetHttpStatusInfo(StatusCode, ref Meaning, ref Cause, ref Resolution);

                //MessageBox.Show("IIS Resposne Code: " + StatusCode + "\r\nDescription: " + Meaning);

                return(response);
            }
            catch (WebException wex)
            {
                MessageBox.Show("Exception: \r\n" + wex.ToString(), "Error");

                return(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: \r\n" + ex.ToString(), "Error");

                return(null);
            }
        }
Example #2
0
        public ASOptionsResponse GetOptions()
        {
            if (server == null)
            {
                throw new InvalidDataException("ASOptionsRequest not initialized because server was not specified.");
            }

            if ((useCertificateAuthentication == false && credential == null) || server == null)
            {
                throw new InvalidDataException("ASOptionsRequest not initialized. User credentials or certificate authentication need to be specified.");
            }

            string         strURI    = string.Format("{0}//{1}/Microsoft-Server-ActiveSync", UseSSL ? "https:" : "http:", Server);
            Uri            serverUri = new Uri(strURI);
            HttpWebRequest httpReq   = (HttpWebRequest)WebRequest.Create(strURI);

            if (useCertificateAuthentication == true)
            {
                try
                {
                    httpReq.UseDefaultCredentials = false;
                    X509Certificate oX509Certificate = null;
                    oX509Certificate = new X509Certificate(certificateFile, certificatePassword);
                    httpReq.ClientCertificates.Add(oX509Certificate);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception: \r\n" + ex.ToString(), "Error setting certificate authentication.");
                    return(null);
                }
            }
            else
            {
                try
                {
                    CredentialCache creds = new CredentialCache();
                    creds.Add(serverUri, "Basic", Credentials);  // Using Basic authentication
                    httpReq.Credentials = creds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception: \r\n" + ex.ToString(), "Error setting specified credentials.");
                    return(null);
                }
            }

            httpReq.Method = "OPTIONS";

            if (SpecifyProxySettings == true)
            {
                WebProxy oWebProxy = null;
                oWebProxy = new WebProxy(ProxyServer, ProxyPort);
                //oWebProxy.BypassProxyOnLocal = BypassProxyForLocalAddress;

                if (OverrideProxyCredntials == true)
                {
                    if (ProxyUser.Trim().Length == 0)
                    {
                        oWebProxy.UseDefaultCredentials = true;
                    }
                    else
                    {
                        if (ProxyDomain.Trim().Length == 0)
                        {
                            oWebProxy.Credentials = new NetworkCredential(ProxyUser, ProxyPassword);
                        }
                        else
                        {
                            oWebProxy.Credentials = new NetworkCredential(ProxyUser, ProxyPassword, ProxyDomain);
                        }
                    }
                }
                else
                {
                    oWebProxy.UseDefaultCredentials = true;
                }

                httpReq.Proxy = oWebProxy;
            }

            if (UserAgent.Trim().Length != 0)
            {
                httpReq.UserAgent = UserAgent;
            }

            try
            {
                HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

                ASOptionsResponse response = new ASOptionsResponse(httpResp);


                httpResp.Close();

                return(response);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error calling GetOptions");
                //VSError.ReportException(ex);
                return(null);
            }
        }