none
NFe RRS feed

  • Pergunta

  • Senhores(as), bom dia.

    Estou precisando consumir web service da SEFAZ para NFe. Alguém pode contribuir mostrando um código que funcione par consumir este web service? Pode ser para qualquer método, pois fiz muitas tentativas e nenhum realmente funciona. Estou adicionando um exemplo:

    Agradeço e ótimo dia a todos.

    private void button18_Click(object sender, EventArgs e)

            {

                X509Certificate2Collection lcerts;

                X509Store lStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

                // Abre o Store

                lStore.Open(OpenFlags.ReadOnly);

                // Lista os certificados

                lcerts = lStore.Certificates;

                foreach (X509Certificate2 cert in lcerts)

                {

                    if (cert.HasPrivateKey && cert.NotAfter > DateTime.Now && cert.NotBefore < DateTime.Now)

                    {

                        // Faz o uso do certificado….

                        // Por exemplo, assinar um docto.

                        //ServicePointManager.Expect100Continue = false;

                        RequestWebService(@"https://homologacao.nfe.fazenda.sp.gov.br/ws/nfeconsulta2.asmx", "1,1", "NfeConsultaProtocolo", cert);

                          

                    }

                }

                lStore.Close();

            }

    public string RequestWebService(string wsURL, string param, string action, X509Certificate2 certificado)

            {

                try

                {

                    Uri urlpost = new Uri(wsURL);

                    HttpWebRequest httpPostConsultaNFe = (HttpWebRequest)HttpWebRequest.Create(urlpost);

                    string postConsultaComParametros = param;

                   

                    string sConteudo;

                    sConteudo = "";

                    sConteudo = sConteudo + "<?xml version='1.0' encoding='utf-8'?>";

                    sConteudo = sConteudo + "<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>";

                    sConteudo = sConteudo + "   <soap12:Header>";

                    sConteudo = sConteudo + "     <nfeCabecMsg xmlns='https://homologacao.nfce.fazenda.sp.gov.br/ws/nfeconsulta2.asmx2'>";

                    sConteudo = sConteudo + "       <versaoDados>5.00</versaoDados>";

                    sConteudo = sConteudo + "       <cUF>35</cUF>";

                    sConteudo = sConteudo + "     </nfeCabecMsg>";

                    sConteudo = sConteudo + "   </soap12:Header>";

                    sConteudo = sConteudo + "   <soap12:Body>";

                    sConteudo = sConteudo + "     <nfeDadosMsg xmlns='https://homologacao.nfce.fazenda.sp.gov.br/ws/nfeconsulta2.asmx'>";

                    sConteudo = sConteudo + "       <ConsCad xmlns='http://www.portalfiscal.inf.br/nfe' versao='3.10'>";

                    sConteudo = sConteudo + "         <infCons>";

                    sConteudo = sConteudo + "           <xServ>CONS-CAD</xServ>";

                    sConteudo = sConteudo + "           <UF>SP</UF>";

                    sConteudo = sConteudo + "           <CNPJ>07859494000103</CNPJ>";

                    sConteudo = sConteudo + "         </infCons>";

                    sConteudo = sConteudo + "       </ConsCad>";

                    sConteudo = sConteudo + "     </nfeDadosMsg>";

                    sConteudo = sConteudo + "   </soap12:Body>";

                    sConteudo = sConteudo + "</soap12:Envelope>";

                    byte[] buffer2 = Encoding.ASCII.GetBytes(sConteudo);

                    //httpPostConsultaNFe.CookieContainer = cookies;

                    httpPostConsultaNFe.Timeout = 300000;

                    httpPostConsultaNFe.ContentType = "application/soap+xml; charset=utf-8; action=" + action;

                    //httpPostConsultaNFe.ContentType = "text/xml";

                    httpPostConsultaNFe.Method = "POST";

                    httpPostConsultaNFe.ClientCertificates.Add(certificado);

                    httpPostConsultaNFe.ContentLength = buffer2.Length;

                    Stream PostData = httpPostConsultaNFe.GetRequestStream();

                    PostData.Write(buffer2, 0, buffer2.Length);

                    PostData.Close();

                    HttpWebResponse responsePost = (HttpWebResponse)httpPostConsultaNFe.GetResponse();

                    Stream istreamPost = responsePost.GetResponseStream();

                    StreamReader strRespotaUrlConsultaNFe = new StreamReader(istreamPost, System.Text.Encoding.ASCII);

                    return strRespotaUrlConsultaNFe.ReadToEnd();

                   

                }

                catch (WebException ex)

                {

                    throw;

                }

            }

    segunda-feira, 22 de maio de 2017 14:39

Respostas