private static string PostData(string epsURL, XmlDocument data, string userName, string password, string friendlyDBName, bool includeAuthCookie)
        {
            string response = "";

            byte[] responseData;

            Uri epsURI = new Uri(epsURL);

            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.Encoding = System.Text.Encoding.UTF8;
                    wc.Headers.Add("Content-Type", "application/xml");

                    if (includeAuthCookie)
                    {
                        Security.Claims.ClaimsPrincipal c = System.Threading.Thread.CurrentPrincipal as Security.Claims.ClaimsPrincipal;
                        if (c != null)
                        {
                            wc.Headers.Add("cookie", c.AuthenticationCookie);
                        }
                    }
                    else if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(friendlyDBName))
                    {
                        using (AuthenticationServiceClient authclient = new AuthenticationServiceClient("WSHttpBinding_IAuthenticationService"))
                        {
                            Dictionary <string, string> props = new Dictionary <string, string>();

                            props.Add("friendlydbname", friendlyDBName);
                            AuthenticationResponse authresponse = authclient.AuthenticateUser(userName, password, props);
                            if (!authresponse.HasError)
                            {
                                wc.Headers.Add("cookie", authresponse.AuthenticationCookie);
                            }
                            else
                            {
                                throw new Exception(authresponse.ErrorMessage);
                            }
                        }
                    }

                    byte[] sentXML = System.Text.Encoding.UTF8.GetBytes(data.OuterXml);
                    responseData = wc.UploadData(epsURI, "POST", sentXML);
                }
                response = System.Text.Encoding.ASCII.GetString(responseData);
            }
            catch (Exception ex)
            {
                response += "\r\n" + ex.ToString();
            }

            return(response);
        }
        private static string RequestData(string epsURL, string userName, string password, string friendlyDBName, bool includeAuthCookie)
        {
            string         response;
            XmlDocument    res_xmldoc = new XmlDocument();
            HttpWebRequest request    = (HttpWebRequest)HttpWebRequest.Create(epsURL);

            request.Method = "GET";

            if (includeAuthCookie)
            {
                Security.Claims.ClaimsPrincipal c = System.Threading.Thread.CurrentPrincipal as Security.Claims.ClaimsPrincipal;
                if (c != null)
                {
                    request.Headers.Add("cookie", c.AuthenticationCookie);
                }
            }
            else if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(friendlyDBName))
            {
                using (AuthenticationServiceClient authclient = new AuthenticationServiceClient("WSHttpBinding_IAuthenticationService"))
                {
                    Dictionary <string, string> props = new Dictionary <string, string>();

                    props.Add("friendlydbname", friendlyDBName);
                    AuthenticationResponse authresponse = authclient.AuthenticateUser(userName, password, props);
                    if (!authresponse.HasError)
                    {
                        request.Headers.Add("cookie", authresponse.AuthenticationCookie);
                    }
                    else
                    {
                        throw new Exception(authresponse.ErrorMessage);
                    }
                }
            }

            using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
            {
                using (Stream res_stream = res.GetResponseStream())
                {
                    // Load the response stream into an xml document
                    res_xmldoc.Load(res_stream);
                    response = res_xmldoc.OuterXml;
                }
            }
            return(response);
        }