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