public static async Task <RetornoSiscomex> EnviarSemServico(string xml, string due, string cpfCertificado)
        {
            try
            {
                try
                {
                    HttpResponseMessage response = new HttpResponseMessage();

                    var token = new Token();

                    token = Autenticar(cpfCertificado);

                    if (token != null)
                    {
                        var headers = ObterHeaders(token);

                        var url = string.Format("/due/api/ext/due/{0}", due);



                        using (var handler = new WebRequestHandler())
                        {
                            handler.UseProxy = false;
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                            handler.ClientCertificates.Add(ObterCertificado(cpfCertificado));
                            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;

                            using (var client = new HttpClient(handler))
                            {
                                // client.Timeout = new TimeSpan(0, 15, 00);
                                client.Timeout = new TimeSpan(25, 0, 0);

                                client.DefaultRequestHeaders.Accept.Clear();
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

                                foreach (var header in headers)
                                {
                                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
                                }

                                client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                                client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip,deflate");
                                client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");

                                using (var stringContent = new StringContent(xml, Encoding.UTF8, "application/xml"))
                                {
                                    HttpResponseMessage retorno = await client
                                                                  .PutAsync(new Uri(string.Concat(UrlSiscomex, url)), stringContent)
                                                                  .ConfigureAwait(false);

                                    response = retorno;
                                }
                            }
                        }
                    }

                    var retornoSiscomex = new ServicoSiscomex.RetornoSiscomex();

                    var retornoResponse = response.Content.ReadAsStringAsync().Result;

                    using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(Regex.Unescape(retornoResponse))))
                    {
                        var reader = new StreamReader(memoryStream, Encoding.UTF8);

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            try
                            {
                                var xmlSerializer = new XmlSerializer(typeof(RetornoCriticas));
                                var criticas      = (ServicoSiscomex.RetornoCriticas)xmlSerializer.Deserialize(reader);

                                retornoSiscomex.Sucesso    = false;
                                retornoSiscomex.Warnings   = false;
                                retornoSiscomex.Message    = criticas.message;
                                retornoSiscomex.Criticas   = criticas.detail;
                                retornoSiscomex.XmlRetorno = retornoResponse;
                            }
                            catch (Exception ex)
                            {
                                retornoSiscomex.Sucesso    = false;
                                retornoSiscomex.Message    = "Erro: " + retornoResponse;
                                retornoSiscomex.XmlRetorno = retornoResponse;
                            }
                        }
                        else
                        {
                            if (retornoResponse.Contains("warnings"))
                            {
                                var xmlSerializer = new XmlSerializer(typeof(PucomexReturn));
                                var warnings      = (PucomexReturn)xmlSerializer.Deserialize(reader);

                                retornoSiscomex.Sucesso  = true;
                                retornoSiscomex.Warnings = true;
                                retornoSiscomex.Message  = string.Join("<br />", warnings.Warnings.Warning);
                            }
                            else
                            {
                                var xmlSerializer = new XmlSerializer(typeof(RetornoSucesso));
                                var sucesso       = (RetornoSucesso)xmlSerializer.Deserialize(reader);

                                retornoSiscomex.Sucesso  = true;
                                retornoSiscomex.Warnings = false;
                                retornoSiscomex.Message  = sucesso.Message;
                                retornoSiscomex.DUE      = sucesso.Due;
                                retornoSiscomex.RUC      = sucesso.Ruc;
                            }
                        }
                    }

                    return(retornoSiscomex);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                return(new RetornoSiscomex
                {
                    Sucesso = false,
                    Message = ex.Message
                });
            }
        }
 private static Dictionary <string, string> ObterHeaders(Token token) => new Dictionary <string, string>
 {
     { "Authorization", token.SetToken },
     { "x-csrf-token", token.CsrfToken }
 };