1

Estou tentando assinar um arquivo xml, de uma NFS-e. Essas são as funções que me foram passadas:

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;
        }
    }

Porém como o código não foi eu que desenvolvi, eu tenho algumas dúvidas, e não sei como passar o certificado para a variável, eu sei dos outros dados, que são desta forma:

    post("http://fi1.fiorilli.com.br:5663/IssWeb-ejb/IssWebWS/IssWebWS?wsdl", "GerarNfseEnvio", "E:\\nota.xml", 
 certificadoaqui);

Como passo para a função o certificado? Pesquisei na internet e não consegui tirar minhas dúvidas.

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .