private XmlNode GetXmlResponse(string URL, string method, Request requestBody)
        {
            try
            {
                var request = Configuration.HttpWebRequestFactory(Environment.GatewayURL + URL);
                request.Headers.Add("Authorization", GetAuthorizationHeader());
                request.Headers.Add("X-ApiVersion", ApiVersion);
                request.Headers.Add("Accept-Encoding", "gzip");
                request.Accept    = "application/xml";
                request.UserAgent = "Braintree .NET " + AssemblyVersion;
                setRequestProxy(request);
                request.Method           = method;
                request.KeepAlive        = false;
                request.Timeout          = Configuration.Timeout;
                request.ReadWriteTimeout = Configuration.Timeout;

                if (requestBody != null)
                {
                    var    xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    byte[] buffer    = Encoding.UTF8.GetBytes(xmlPrefix + requestBody.ToXml());
                    request.ContentType   = "application/xml";
                    request.ContentLength = buffer.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    return(ParseResponseStream(GetResponseStream(response)));
                }
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse)e.Response)
                {
                    if (response == null)
                    {
                        throw e;
                    }

                    var statusCode = response.StatusCode;
                    if (statusCode == (HttpStatusCode)422) // UnprocessableEntity
                    {
                        XmlNode doc = ParseResponseStream(GetResponseStream(response));
                        return(doc);
                    }

                    ThrowExceptionIfErrorStatusCode(statusCode, null);
                }

                throw e;
            }
        }
        public HttpWebRequest GetHttpRequest(string URL, string method) {
            const int SecurityProtocolTypeTls12 = 3072;
            ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | ((SecurityProtocolType) SecurityProtocolTypeTls12);

            var request = Configuration.HttpWebRequestFactory(URL);
            SetRequestProxy(request);
            SetRequestHeaders(request);
            request.Method = method;
            request.KeepAlive = false;
            request.Timeout = Configuration.Timeout;
            request.ReadWriteTimeout = Configuration.Timeout;

            return request;
        }
        private XmlNode GetXmlResponse(string URL, string method, Request requestBody)
        {
#if netcore
            try
            {
                var request = Configuration.HttpRequestMessageFactory(new HttpMethod(method), Environment.GatewayURL + URL);
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(GetAuthorizationSchema(), GetAuthorizationHeader());

                request.Headers.Add("X-ApiVersion", ApiVersion);
                request.Headers.Add("AcceptEncoding", "gzip");
                request.Headers.Add("Accept", "application/xml");
                request.Headers.Add("UserAgent", "Braintree .NET " + typeof(BraintreeService).GetTypeInfo().Assembly.GetName().Version.ToString());
                request.Headers.Add("Keep-Alive", "false");

                if (requestBody != null)
                {
                    var xmlPrefix   = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    var content     = xmlPrefix + requestBody.ToXml();
                    var utf8_string = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(content));
                    request.Content = new StringContent(utf8_string, Encoding.UTF8, "application/xml");
                    request.Content.Headers.ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(utf8_string);
                }

                var httpClientHandler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                };

                SetWebProxy(httpClientHandler, URL);

                XmlNode doc = new XmlDocument();
                using (var client = new HttpClient(httpClientHandler))
                {
                    client.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);
                    var response = client.SendAsync(request).GetAwaiter().GetResult();

                    if (response.StatusCode != (HttpStatusCode)422)
                    {
                        ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                    }

                    doc = ParseResponseStream(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
                }

                return(doc);
            }
            catch (HttpRequestException e)
            {
                throw e;
            }
#else
            try
            {
                var request = Configuration.HttpWebRequestFactory(Environment.GatewayURL + URL);
                request.Headers.Add("Authorization", GetAuthorizationHeader());
                request.Headers.Add("X-ApiVersion", ApiVersion);
                request.Headers.Add("Accept-Encoding", "gzip");
                request.Accept    = "application/xml";
                request.UserAgent = "Braintree .NET " + typeof(BraintreeService).Assembly.GetName().Version.ToString();
                setRequestProxy(request);
                request.Method           = method;
                request.KeepAlive        = false;
                request.Timeout          = Configuration.Timeout;
                request.ReadWriteTimeout = Configuration.Timeout;

                if (requestBody != null)
                {
                    var    xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    byte[] buffer    = Encoding.UTF8.GetBytes(xmlPrefix + requestBody.ToXml());
                    request.ContentType   = "application/xml";
                    request.ContentLength = buffer.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();
                }

                var response = request.GetResponse() as HttpWebResponse;

                XmlNode doc = ParseResponseStream(GetResponseStream(response));
                response.Close();

                return(doc);
            }
            catch (WebException e)
            {
                var response = (HttpWebResponse)e.Response;
                if (response == null)
                {
                    throw e;
                }

                if (response.StatusCode == (HttpStatusCode)422) // UnprocessableEntity
                {
                    XmlNode doc = ParseResponseStream(GetResponseStream((HttpWebResponse)e.Response));
                    e.Response.Close();
                    return(doc);
                }

                ThrowExceptionIfErrorStatusCode(response.StatusCode, null);

                throw e;
            }
#endif
        }
        private XmlNode GetXmlResponse(string URL, string method, Request requestBody)
        {
#if netcore
            try
            {
                var request = Configuration.HttpRequestMessageFactory(new HttpMethod(method), Environment.GatewayURL + URL);
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(GetAuthorizationSchema(), GetAuthorizationHeader());

                SetRequestHeaders(request);

                if (requestBody != null)
                {
                    var xmlPrefix   = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    var content     = xmlPrefix + requestBody.ToXml();
                    var utf8_string = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(content));
                    request.Content = new StringContent(utf8_string, Encoding.UTF8, "application/xml");
                    request.Content.Headers.ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(utf8_string);
                }

                var httpClientHandler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                };

                SetWebProxy(httpClientHandler, URL);

                using (var client = new HttpClient(httpClientHandler))
                {
                    client.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);
                    var response = client.SendAsync(request).GetAwaiter().GetResult();

                    if (response.StatusCode != (HttpStatusCode)422)
                    {
                        ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                    }

                    return(ParseResponseStream(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()));
                }
            }
            catch (HttpRequestException e)
            {
                throw e;
            }
#else
            try
            {
                const int SecurityProtocolTypeTls12 = 3072;
                ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | ((SecurityProtocolType)SecurityProtocolTypeTls12);
                var request = Configuration.HttpWebRequestFactory(Environment.GatewayURL + URL);
                SetRequestHeaders(request);
                setRequestProxy(request);
                request.Method           = method;
                request.KeepAlive        = false;
                request.Timeout          = Configuration.Timeout;
                request.ReadWriteTimeout = Configuration.Timeout;

                if (requestBody != null)
                {
                    var    xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    byte[] buffer    = Encoding.UTF8.GetBytes(xmlPrefix + requestBody.ToXml());
                    request.ContentType   = "application/xml";
                    request.ContentLength = buffer.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    return(ParseResponseStream(GetResponseStream(response)));
                }
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse)e.Response)
                {
                    if (response == null)
                    {
                        throw e;
                    }

                    if (response.StatusCode == (HttpStatusCode)422) // UnprocessableEntity
                    {
                        return(ParseResponseStream(GetResponseStream((HttpWebResponse)e.Response)));
                    }

                    ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                }

                throw e;
            }
#endif
        }
        private XmlNode GetXmlResponse(string URL, string method, Request requestBody, FileStream file)
        {
#if netcore
            try
            {
                var request = Configuration.HttpRequestMessageFactory(new HttpMethod(method), Environment.GatewayURL + URL);
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(GetAuthorizationSchema(), GetAuthorizationHeader());

                SetRequestHeaders(request);

                if (requestBody != null && file == null)
                {
                    var xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    var content = xmlPrefix + requestBody.ToXml();
                    var utf8_string = encoding.GetString(encoding.GetBytes(content));
                    request.Content = new StringContent(utf8_string, encoding, "application/xml");
                    request.Content.Headers.ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(utf8_string);
                }

                if (file != null)
                {
                    string formDataBoundary = GenerateMultipartFormBoundary();

                    Dictionary<string, object> postParameters = requestBody.ToDictionary();
                    postParameters.Add("file", file);

                    byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
                    var ascii_string = Encoding.ASCII.GetString(formData);
                    request.Content = new StringContent(ascii_string);
                    request.Content.Headers.Remove("Content-Type");
                    request.Content.Headers.TryAddWithoutValidation("Content-Type", MultipartFormContentType(formDataBoundary));
                    request.Content.Headers.ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(ascii_string);
                }

                var httpClientHandler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                };

                SetWebProxy(httpClientHandler, Environment.GatewayURL + URL);

                using (var client = new HttpClient(httpClientHandler))
                {
                    client.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);
                    var response = client.SendAsync(request).GetAwaiter().GetResult();

                    if (response.StatusCode != (HttpStatusCode)422)
                    {
                        ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                    }

                    return ParseResponseStream(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
                }
            }
            catch (HttpRequestException e)
            {
                throw e;
            }
#else
            try
            {
                const int SecurityProtocolTypeTls12 = 3072;
                ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | ((SecurityProtocolType)SecurityProtocolTypeTls12);
                var request = Configuration.HttpWebRequestFactory(Environment.GatewayURL + URL);
                SetRequestHeaders(request);
                setRequestProxy(request);
                request.Method = method;
                request.KeepAlive = false;
                request.Timeout = Configuration.Timeout;
                request.ReadWriteTimeout = Configuration.Timeout;

                if (requestBody != null && file == null)
                {
                    var xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    byte[] buffer = encoding.GetBytes(xmlPrefix + requestBody.ToXml());
                    request.ContentType = "application/xml";
                    request.ContentLength = buffer.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
                }

                if (file != null)
                {
                    string formDataBoundary = GenerateMultipartFormBoundary();
                    request.ContentType = MultipartFormContentType(formDataBoundary);

                    Dictionary<string, object> postParameters = requestBody.ToDictionary();
                    postParameters.Add("file", file);

                    byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
                    request.ContentLength = formData.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(formData, 0, formData.Length);
                        requestStream.Close();
                    }
                }

                using (var response = (HttpWebResponse) request.GetResponse())
                {
                    return ParseResponseStream(GetResponseStream(response));
                }
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse) e.Response)
                {
                    if (response == null) throw e;

                    if (response.StatusCode == (HttpStatusCode)422) // UnprocessableEntity
                    {
                        return ParseResponseStream(GetResponseStream((HttpWebResponse)e.Response));
                    }

                    ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                }

                throw e;
            }
#endif
        }