Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ajustar para pegar da nova estrutura a comunicação com a sefaz,… #1496

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading