Skip to content

Commit

Permalink
Merge pull request #1496 from ZeusAutomacao/feat/nova-estrutura-ajust…
Browse files Browse the repository at this point in the history
…e-para-pegar-da-mesma

feat: ajustar para pegar da nova estrutura a comunicação com a sefaz,…
  • Loading branch information
robertorp authored Feb 18, 2024
2 parents 5f03b43 + 9e0b801 commit 059dd62
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 104 deletions.
104 changes: 99 additions & 5 deletions DFe.Wsdl/Common/RequestSefazDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,31 @@ public class RequestSefazDefault : IRequestSefaz
/// <returns></returns>
public XmlDocument SerealizeDocument<T>(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;
}

/// <summary>
Expand All @@ -36,14 +60,84 @@ public XmlDocument SerealizeDocument<T>(T soapEnvelope)
/// <returns></returns>
public async Task<string> 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;
}
}
}
}
102 changes: 3 additions & 99 deletions DFe.Wsdl/Common/SoapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,7 @@ public class SoapUtils
/// <returns></returns>
public XmlDocument SerealizeDocument<T>(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);
}

/// <summary>
Expand All @@ -64,84 +40,12 @@ public XmlDocument SerealizeDocument<T>(T soapEnvelope)
/// <returns></returns>
public async Task<string> 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);
}
}

Expand Down

0 comments on commit 059dd62

Please sign in to comment.