I'm trying to sign an xml file, from an NFS-e. Here are the functions I've been given:
public static MemoryStream stringToStream(string dados)
{
MemoryStream memoryStream;
try
{
byte[] byteArray = new byte[dados.Length];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byteArray = encoding.GetBytes(dados);
memoryStream = new MemoryStream(byteArray);
memoryStream.Seek(0, SeekOrigin.Begin);
}
catch (Exception ex)
{
return null;
}
return memoryStream;
}
private void montaEnvelope(HttpWebRequest webRequest, XmlDocument document)
{
string soapEnvelope = string.Empty;
soapEnvelope += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.issweb.fiorilli.com.br/\" xmlns:xd=\"http://www.w3.org/2000/09/xmldsig#\">";
soapEnvelope += "<soapenv:Header/><soapenv:Body><ws:gerarNfse>";
soapEnvelope += document.LastChild.OuterXml.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", string.Empty);
soapEnvelope += "<username>01001001000113</username><password>123456</password></ws:gerarNfse></soapenv:Body></soapenv:Envelope>";
MemoryStream stream = stringToStream(soapEnvelope);
webRequest.ContentLength = stream.Length;
Stream requestStream = webRequest.GetRequestStream();
stream.WriteTo(requestStream);
document.LoadXml(soapEnvelope);
document.Save(@"E:\_Xml.xml");
this.XmlDocNFSe = document;
}
private static string getSoapResponse(HttpWebRequest webRequest)
{
string soapResult = string.Empty;
WebResponse webResponse = webRequest.GetResponse();
StreamReader rd = new StreamReader(webResponse.GetResponseStream());
soapResult = rd.ReadToEnd();
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(soapResult);
XmlNode responseNode = xmlResponse.LastChild.LastChild.FirstChild;
return responseNode.InnerXml;
}
private static HttpWebRequest getWebRequest(string url, string method)
{
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.Timeout = 100000;
webRequest.Headers.Add("SOAPAction", method);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private string post(string url, string method, string xml, X509Certificate2 certificate)
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
HttpWebRequest webRequest = getWebRequest(url, method);
webRequest.ClientCertificates.Add(certificate);
montaEnvelope(webRequest, doc);
return getSoapResponse(webRequest);
}
catch (Exception ex)
{
return null;
}
}
But as the code was not the one I developed, I have some doubts, and I do not know how to pass the certificate to the variable, I know of the other data, which is like this:
post("http://fi1.fiorilli.com.br:5663/IssWeb-ejb/IssWebWS/IssWebWS?wsdl", "GerarNfseEnvio", "E:\nota.xml",
certificadoaqui);
How do I step to the certificate function? I researched the internet and could not get my doubts.