private ResultadoHttpRest RetornoRequisicao(HttpResponseMessage actionHttpResponse)
        {
            ResultadoHttpRest ret = new ResultadoHttpRest();

            ret.Conteudo = actionHttpResponse.Content.ReadAsStringAsync().Result;

            var mediaTypeJson = Selia.Integrador.Utils.Constants.AppSettings.MediaTypeHeaderJson;

            if (actionHttpResponse.Content.Headers.ContentType == null)
            {
                ret.ConteudoJson = true;
                ret.Conteudo     = "{ \"Result\" : true }";
            }
            else
            {
                ret.ConteudoJson = actionHttpResponse.Content.Headers.ContentType.MediaType.ToString().Equals(mediaTypeJson);
            }

            var listaCodeSucesso = Selia.Integrador.Utils.Constants.AppSettings.HttpStatusCode.Sucesso.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            var respostaStatusCode = ((int)actionHttpResponse.StatusCode).ToString();

            if (!listaCodeSucesso.Any(a => a == respostaStatusCode))
            {
                throw new Selia.Integrador.Utils.Exceptions.HttpRestException(message: ret.Conteudo, informacaoAdicional: "STATUS: " + ret.HttpStatusCodeSucess + " " + ret.Conteudo, retornoNaoTratado: ret.Conteudo);
            }
            return(ret);
        }
Пример #2
0
        public ResultadoHttpRest FazerRequisicao(Func <HttpResponseMessage, ResultadoHttpRest> actionHttpResponse)
        {
            ResultadoHttpRest ret = null;

            if (string.IsNullOrEmpty(Endereco))
            {
                throw new Exceptions.HttpRestException("Endereço do serviço Rest é obrigatório");
            }


            System.Net.Http.HttpClient client  = null;
            HttpClientHandler          handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            ServicePointManager.Expect100Continue = false;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            if (!string.IsNullOrEmpty(Login) && !string.IsNullOrEmpty(Senha))
            {
                ICredentials myCredentials = new NetworkCredential(this.Login, this.Senha);
                handler.Credentials = myCredentials;
                client = new HttpClient(handler);
            }
            else
            {
                client = new HttpClient(handler);
            }

            var httpMethod = ConverterHttpMethod(httpVerbo);

            try
            {
                using (var request = new HttpRequestMessage(httpMethod, Endereco))
                {
                    if (!string.IsNullOrEmpty(Data))
                    {
                        request.Content = new StringContent(Data, Encoding.UTF8, ContentytType);
                    }
                    //Preenche os cabeçalhos se necessário
                    PreencherCabecalhoRequisicao(request);

                    //if (AutenticacaoRest != null)
                    //{
                    //    MontaAutenticacaoRest(client);
                    //}

                    using (var response = client.SendAsync(request).Result)
                    {
                        if (actionHttpResponse != null)
                        {
                            ret = actionHttpResponse(response);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                client.Dispose();
            }


            return(ret);
        }