From 9e0b801454605854a9471b9fe40f28f8475c0d2c Mon Sep 17 00:00:00 2001 From: ROBERTO ALVES PEREIRA Date: Sun, 18 Feb 2024 15:08:10 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20ajustar=20para=20pegar=20da=20nova=20es?= =?UTF-8?q?trutura=20a=20comunica=C3=A7=C3=A3o=20com=20a=20sefaz,=20sendo?= =?UTF-8?q?=20a=20possibilidade=20de=20implementar=20customiza=C3=A7=C3=B5?= =?UTF-8?q?es=20pessoais?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DFe.Wsdl/Common/RequestSefazDefault.cs | 104 +++++++++++++++++++++++-- DFe.Wsdl/Common/SoapUtils.cs | 102 +----------------------- 2 files changed, 102 insertions(+), 104 deletions(-) diff --git a/DFe.Wsdl/Common/RequestSefazDefault.cs b/DFe.Wsdl/Common/RequestSefazDefault.cs index 6773705c5..92d470983 100644 --- a/DFe.Wsdl/Common/RequestSefazDefault.cs +++ b/DFe.Wsdl/Common/RequestSefazDefault.cs @@ -22,7 +22,31 @@ public class RequestSefazDefault : IRequestSefaz /// public XmlDocument SerealizeDocument(T soapEnvelope) { - return ConfiguracaoServicoWSDL.RequestSefaz.SerealizeDocument(soapEnvelope); + // instancia do objeto responsável pela serialização + XmlSerializer soapserializer = new XmlSerializer(typeof(T)); + + // Armazena os dados em memória para manipulação + MemoryStream memoryStream = new MemoryStream(); + XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); + + //Serializa o objeto de acordo com o formato + soapserializer.Serialize(xmlTextWriter, soapEnvelope); + xmlTextWriter.Formatting = Formatting.None; + + XmlDocument xmlDocument = new XmlDocument(); + + //Remove o caractere especial BOM (byte order mark) + string output = Encoding.UTF8.GetString(memoryStream.ToArray()); + string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); + if (output.StartsWith(_byteOrderMarkUtf8)) + { + output = output.Remove(0, _byteOrderMarkUtf8.Length); + } + + //Carrega os dados na instancia do XmlDocument + xmlDocument.LoadXml(output); + + return xmlDocument; } /// @@ -36,14 +60,84 @@ public XmlDocument SerealizeDocument(T soapEnvelope) /// public async Task SendRequestAsync(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento? tipoEvento = null, string actionUrn = "") { - return await ConfiguracaoServicoWSDL.RequestSefaz.SendRequestAsync(xmlEnvelop, certificadoDigital, url, timeOut, - tipoEvento, actionUrn); + //verifica se pelo menos uma das 2 propriedades obrigatorias estão definidas + if (!tipoEvento.HasValue && string.IsNullOrWhiteSpace(actionUrn)) + { + throw new ArgumentNullException("Pelo menos uma das propriedades tipoEvento ou actionUrl devem ser definidos para executar a action na requisição soap"); + } + + //caso o tipoevento esteja definido, pega a url do evento + if (tipoEvento.HasValue) + { + actionUrn = new SoapUrls().GetSoapUrl(tipoEvento.Value); + } + + string xmlSoap = xmlEnvelop.InnerXml; + HttpWebRequest httpWr = (HttpWebRequest)WebRequest.Create(new Uri(url)); + + if (ConfiguracaoServicoWSDL.ValidarCertificadoDoServidorNetCore == false) + { + httpWr.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; + } + + httpWr.Timeout = timeOut == 0 ? 2000 : timeOut; + httpWr.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length; + httpWr.ClientCertificates.Add(certificadoDigital); + httpWr.ComposeContentType("application/soap+xml", Encoding.UTF8, actionUrn); + httpWr.Method = "POST"; + + StreamWriter streamWriter = new StreamWriter(httpWr.GetRequestStream()); + + streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); + streamWriter.Close(); + + using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + string xmlRetorno = streamReader.ReadToEnd(); + return await Task.FromResult(xmlRetorno); + } } public string SendRequest(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento? tipoEvento = null, string actionUrn = "") { - return ConfiguracaoServicoWSDL.RequestSefaz.SendRequest(xmlEnvelop, certificadoDigital, url, timeOut, - tipoEvento, actionUrn); + //verifica se pelo menos uma das 2 propriedades obrigatorias estão definidas + if (!tipoEvento.HasValue && string.IsNullOrWhiteSpace(actionUrn)) + { + throw new ArgumentNullException("Pelo menos uma das propriedades tipoEvento ou actionUrl devem ser definidos para executar a action na requisição soap"); + } + + //caso o tipoevento esteja definido, pega a url do evento + if (tipoEvento.HasValue) + { + actionUrn = new SoapUrls().GetSoapUrl(tipoEvento.Value); + } + + string xmlSoap = xmlEnvelop.InnerXml; + HttpWebRequest httpWr = (HttpWebRequest)WebRequest.Create(new Uri(url)); + + if (ConfiguracaoServicoWSDL.ValidarCertificadoDoServidorNetCore == false) + { + httpWr.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; + } + + httpWr.Timeout = timeOut == 0 ? 2000 : timeOut; + httpWr.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length; + httpWr.ClientCertificates.Add(certificadoDigital); + httpWr.ComposeContentType("application/soap+xml", Encoding.UTF8, actionUrn); + httpWr.Method = "POST"; + + StreamWriter streamWriter = new StreamWriter(httpWr.GetRequestStream()); + + streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); + streamWriter.Close(); + + using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + string xmlRetorno = streamReader.ReadToEnd(); + return xmlRetorno; + } } } } \ No newline at end of file diff --git a/DFe.Wsdl/Common/SoapUtils.cs b/DFe.Wsdl/Common/SoapUtils.cs index b39e8d379..2e986ce66 100644 --- a/DFe.Wsdl/Common/SoapUtils.cs +++ b/DFe.Wsdl/Common/SoapUtils.cs @@ -26,31 +26,7 @@ public class SoapUtils /// public XmlDocument SerealizeDocument(T soapEnvelope) { - // instancia do objeto responsável pela serialização - XmlSerializer soapserializer = new XmlSerializer(typeof(T)); - - // Armazena os dados em memória para manipulação - MemoryStream memoryStream = new MemoryStream(); - XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); - - //Serializa o objeto de acordo com o formato - soapserializer.Serialize(xmlTextWriter, soapEnvelope); - xmlTextWriter.Formatting = Formatting.None; - - XmlDocument xmlDocument = new XmlDocument(); - - //Remove o caractere especial BOM (byte order mark) - string output = Encoding.UTF8.GetString(memoryStream.ToArray()); - string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); - if (output.StartsWith(_byteOrderMarkUtf8)) - { - output = output.Remove(0, _byteOrderMarkUtf8.Length); - } - - //Carrega os dados na instancia do XmlDocument - xmlDocument.LoadXml(output); - - return xmlDocument; + return ConfiguracaoServicoWSDL.RequestSefaz.SerealizeDocument(soapEnvelope); } /// @@ -64,84 +40,12 @@ public XmlDocument SerealizeDocument(T soapEnvelope) /// public async Task SendRequestAsync(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento? tipoEvento = null, string actionUrn = "") { - //verifica se pelo menos uma das 2 propriedades obrigatorias estão definidas - if (!tipoEvento.HasValue && string.IsNullOrWhiteSpace(actionUrn)) - { - throw new ArgumentNullException("Pelo menos uma das propriedades tipoEvento ou actionUrl devem ser definidos para executar a action na requisição soap"); - } - - //caso o tipoevento esteja definido, pega a url do evento - if (tipoEvento.HasValue) - { - actionUrn = new SoapUrls().GetSoapUrl(tipoEvento.Value); - } - - string xmlSoap = xmlEnvelop.InnerXml; - HttpWebRequest httpWr = (HttpWebRequest)WebRequest.Create(new Uri(url)); - - if (ConfiguracaoServicoWSDL.ValidarCertificadoDoServidorNetCore == false) - { - httpWr.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; - } - - httpWr.Timeout = timeOut == 0 ? 2000 : timeOut; - httpWr.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length; - httpWr.ClientCertificates.Add(certificadoDigital); - httpWr.ComposeContentType("application/soap+xml", Encoding.UTF8, actionUrn); - httpWr.Method = "POST"; - - StreamWriter streamWriter = new StreamWriter(httpWr.GetRequestStream()); - - streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); - streamWriter.Close(); - - using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - { - string xmlRetorno = streamReader.ReadToEnd(); - return await Task.FromResult(xmlRetorno); - } + return await ConfiguracaoServicoWSDL.RequestSefaz.SendRequestAsync(xmlEnvelop, certificadoDigital, url, timeOut, tipoEvento, actionUrn); } public string SendRequest(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento? tipoEvento = null, string actionUrn = "") { - //verifica se pelo menos uma das 2 propriedades obrigatorias estão definidas - if (!tipoEvento.HasValue && string.IsNullOrWhiteSpace(actionUrn)) - { - throw new ArgumentNullException("Pelo menos uma das propriedades tipoEvento ou actionUrl devem ser definidos para executar a action na requisição soap"); - } - - //caso o tipoevento esteja definido, pega a url do evento - if (tipoEvento.HasValue) - { - actionUrn = new SoapUrls().GetSoapUrl(tipoEvento.Value); - } - - string xmlSoap = xmlEnvelop.InnerXml; - HttpWebRequest httpWr = (HttpWebRequest)WebRequest.Create(new Uri(url)); - - if (ConfiguracaoServicoWSDL.ValidarCertificadoDoServidorNetCore == false) - { - httpWr.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; - } - - httpWr.Timeout = timeOut == 0 ? 2000 : timeOut; - httpWr.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length; - httpWr.ClientCertificates.Add(certificadoDigital); - httpWr.ComposeContentType("application/soap+xml", Encoding.UTF8, actionUrn); - httpWr.Method = "POST"; - - StreamWriter streamWriter = new StreamWriter(httpWr.GetRequestStream()); - - streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); - streamWriter.Close(); - - using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - { - string xmlRetorno = streamReader.ReadToEnd(); - return xmlRetorno; - } + return ConfiguracaoServicoWSDL.RequestSefaz.SendRequest(xmlEnvelop, certificadoDigital, url, timeOut, tipoEvento, actionUrn); } }