private async Task <XmlNode> GetXmlResponseAsync(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)await request.GetResponseAsync())
                {
                    return(await ParseResponseStreamAsync(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
                    {
                        // TODO in C# 6 can await in catch block
                        XmlNode doc = ParseResponseStream(GetResponseStream(response));
                        return(doc);
                    }

                    ThrowExceptionIfErrorStatusCode(statusCode, null);
                }

                throw e;
            }
        }
        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 " + 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;
            }
        }
        private XmlNode GetXmlResponse(string URL, string method, Request requestBody)
        {
            try
            {
                var request = new HttpRequestMessage(new HttpMethod(method), Environment.GatewayURL + URL);

                request.Headers.Add("Authorization", GetAuthorizationHeader());
                request.Headers.Add("X-ApiVersion", ApiVersion);
                request.Headers.Add("Accept-Encoding", "gzip");
                request.Headers.Accept.ParseAdd("application/xml");
                request.Headers.UserAgent.ParseAdd("Braintree .NET " + typeof(BraintreeService).GetTypeInfo().Assembly.GetName().Version);



                if (requestBody != null)
                {
                    var xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

                    request.Content = new StringContent(xmlPrefix + requestBody.ToXml());
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
                }

                var proxy = GetProxy();
                var decompressionHandler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    Proxy    = !string.IsNullOrEmpty(proxy) ? new BasicProxy(proxy) : null,
                    UseProxy = !string.IsNullOrEmpty(proxy)
                };
#if netcoreapp10
                WinHttpHandler handler = new WinHttpHandler();
                SetServerCertificateValidationCallback(handler);
#endif
                var client = new HttpClient(decompressionHandler)
                {
                    Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout),
                };

                var response = client.SendAsync(request).GetAwaiter().GetResult();


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

                ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                response.EnsureSuccessStatusCode();
                return(doc);
            }
            catch (HttpRequestException e)
            {
                throw new ServerException();
            }
        }
        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 async Task <XmlNode> GetXmlResponseAsync(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(await ParseResponseStreamAsync(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 = await request.GetRequestStreamAsync().ConfigureAwait(false))
                    {
                        await requestStream.WriteAsync(buffer, 0, buffer.Length);
                    }
                }

                using (var response = (HttpWebResponse)await request.GetResponseAsync().ConfigureAwait(false))
                {
                    return(await ParseResponseStreamAsync(GetResponseStream(response)));
                }
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse)e.Response)
                {
                    if (response == null)
                    {
                        throw e;
                    }

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

                    ThrowExceptionIfErrorStatusCode(response.StatusCode, null);
                }

                throw e;
            }
#endif
        }
        private XmlNode GetXmlResponse(string URL, string method, Request requestBody)
        {
            try
            {
                var request = WebRequest.Create(Environment.GatewayURL + URL) as HttpWebRequest;
                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();
                request.Proxy = new WebProxy(GetProxy());
                request.Method = method;
                request.KeepAlive = false;
                request.Timeout = 60000;
                request.ReadWriteTimeout = 60000;

                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;
            }
        }
Esempio n. 7
0
        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
        }
Esempio n. 8
0
        private async Task <XmlNode> GetXmlResponseAsync(string URL, string method, Request requestBody, FileStream file)
        {
#if netcore
            try
            {
                var request = GetHttpRequest(Environment.GatewayURL + URL, method);

                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 response = await GetHttpResponseAsync(request).ConfigureAwait(false);

                return(StringToXmlNode(response));
            }
            catch (HttpRequestException e)
            {
                throw e;
            }
#else
            var request = GetHttpRequest(Environment.GatewayURL + URL, method);

            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 = await request.GetRequestStreamAsync().ConfigureAwait(false))
                {
                    await requestStream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                }
            }

            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();
                }
            }

            var response = await GetHttpResponseAsync(request).ConfigureAwait(false);

            return(StringToXmlNode(response));
#endif
        }