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

Update IPMServiceClient.cs #83

Merged
merged 1 commit into from
Jun 1, 2022
Merged
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
78 changes: 73 additions & 5 deletions src/OpenAC.Net.NFSe/Providers/IPM/IPMServiceClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ***********************************************************************
// ***********************************************************************
// Assembly : OpenAC.Net.NFSe
// Author : Felipe Silveira (Transis Software)
// Created : 30-05-2022
Expand Down Expand Up @@ -32,8 +32,11 @@

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Xml.Linq;
using OpenAC.Net.Core.Extensions;
using OpenAC.Net.DFe.Core;

namespace OpenAC.Net.NFSe.Providers
{
Expand All @@ -49,7 +52,7 @@ public IPMServiceClient(ProviderBase provider, TipoUrl tipoUrl) : base(provider,

#region Methods

public string EnviarSincrono(string cabec, string msg) => Post(msg, "multipart/form-data");
public string EnviarSincrono(string cabec, string msg) => Post(msg);

public string ConsultarNFSeRps(string cabec, string msg)
{
Expand Down Expand Up @@ -91,7 +94,7 @@ protected override string Authentication()
return "Basic " + base64EncodedAuthenticationString;
}

protected string Post(string message, string contentyType)
protected string Post(string message)
{
var url = Url;

Expand All @@ -102,7 +105,7 @@ protected string Post(string message, string contentyType)

EnvelopeEnvio = message;

Execute(contentyType, "POST", headers);
ExecuteUpload("POST", headers);
return EnvelopeRetorno;
}
finally
Expand All @@ -111,7 +114,72 @@ protected string Post(string message, string contentyType)
}
}

protected void ExecuteUpload(string method, NameValueCollection headers = null)
{
var protocolos = ServicePointManager.SecurityProtocol;
ServicePointManager.SecurityProtocol = Provider.Configuracoes.WebServices.Protocolos;

try
{
string NomeArquivo = $"{DateTime.Now:yyyyMMddssfff}_{PrefixoEnvio}_envio.xml";
if (!EnvelopeEnvio.IsEmpty())
GravarSoap(EnvelopeEnvio, NomeArquivo);

string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

var request = WebRequest.CreateHttp(Url);
request.Method = method.IsEmpty() ? "POST" : method;
request.ContentType = "multipart/form-data; boundary=" + boundary;

if (!ValidarCertificadoServidor())
request.ServerCertificateValidationCallback += (_, _, _, _) => true;

if (Provider.TimeOut.HasValue)
request.Timeout = Provider.TimeOut.Value.Milliseconds;

if (headers?.Count > 0)
request.Headers.Add(headers);


if (Certificado != null)
request.ClientCertificates.Add(Certificado);

Stream rs = request.GetRequestStream();

FileStream fileStream = new FileStream(NomeArquivo, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();

byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
//if (!EnvelopeEnvio.IsEmpty())
//{
// using var streamWriter = new StreamWriter(request.GetRequestStream());
// streamWriter.Write(EnvelopeEnvio);
// streamWriter.Flush();
//}

var response = request.GetResponse();
EnvelopeRetorno = GetResponse(response);

GravarSoap(EnvelopeRetorno, $"{DateTime.Now:yyyyMMddssfff}_{PrefixoResposta}_retorno.xml");
}
catch (Exception ex) when (ex is not OpenDFeCommunicationException)
{
throw new OpenDFeCommunicationException(ex.Message, ex);
}
finally
{
ServicePointManager.SecurityProtocol = protocolos;
}
}

#endregion Methods
}
}
}