1
I am working on sending information from EFD-Reinf and I am facing shipping problems.
Based on the code posted here to make the shipment, however always falls in the catch
of WebException
with the message below:
The SSL Connection could not be established, see Inner Exception. The remote Certificate is invalid According to the validation Procedure.
However, the certificate I am using is valid because it is not expired and the customer uses it to send information.
Below is the code used:
private string ExecuteTransmission(string filePath)
{
X509Certificate2 digitalCertificate;
XmlDocument xmlFile;
XmlTextWriter xmlWriter;
HttpWebRequest request;
string certPath = "AQUI VAI O CAMINHO COMPLETO DO CERTIFICADO";
string password = "SENHA DO CERTIFICADO";
xmlFile = new XmlDocument();
xmlFile.Load(filePath.Trim());
xmlWriter = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlFile.Save(xmlWriter);
xmlWriter.Close();
xmlWriter = null;
request = (HttpWebRequest)WebRequest.Create(_urlToSend);
request.Headers.Add("SOAPAction", _urlToSend);
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";
digitalCertificate = ReinfUtil.GetCertificate(certPath, password);
DigitalcCertificateExtension.CheckValidate(digitalCertificate);
if (digitalCertificate != null)
request.ClientCertificates.Add(digitalCertificate);
// if (this.TimeOutEmSegundos > 0)
// request.Timeout = this.TimeOutEmSegundos * 1000;
try
{
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stream))
{
stmw.Write(xmlFile.InnerXml);
}
}
}
catch (Exception wex)
{
throw new Exception(wex.Message);
}
try
{
WebResponse webresponse = request.GetResponse();
HttpWebResponse response = (HttpWebResponse)webresponse;
//if(response.StatusCode == HttpStatusCode.OK)
using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
{
string result = responseReader.ReadToEnd();
return result;
}
}
catch (WebException wex)
{
string subjacentMesssage = ReinfUtil.GetSubjacentMessage(wex);
string msg = string.Format("{1}\r\n{0}\r\n Detalhes subjacentes: \r\n{2}",
System.Environment.NewLine, wex.Message, subjacentMesssage);
throw new BusinessException(msg);
// throw new Exception(wex.Message);
}
catch
{
throw;
}
}
EDITION
From what I see this message happens because the recipe address itself has an invalid certificate. I did not expect this to happen. I’ll take another look at their documentation and see if there’s anything to be done to overcome the problem if I won’t try to ignore the security exception to run the request anyway.
The code I based is available at :https://answall.com/questions/269338/problema-para-envio-sped-reinf-c?rq=1
– user1927055