示例#1
0
        private async Task <XmlNode> GetXmlResponseAsync(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(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 && 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);
                    }
                }

                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)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
        }
示例#2
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
        }