Simple National Consultation (by CNPJ) - C #

5

I am implementing in C # an Enterprise Query Opting for the Simple National Tax Regime through this link link

I have already implemented another similar query (search the cadastral data of the CNPJ informed) and tried to base it on the same structure of search ... however when I implement the POST in the cited URL a 404 error (not found) is returned. . I do not know much about WEB protocols so could anyone help me? I have problems with the Cookie and also with sending the data to query ... I am testing with a Revenue URL that returns me the CAPTCHA, but I must have problems with this too ...

    public class ConsultaCNPJSimplesNacional
{

    //public static int c=0;
    private readonly CookieContainer _cookies = new CookieContainer();
    private const String urlBaseReceitaFederalCNPJ = "http://www8.receita.fazenda.gov.br/SimplesNacional/Aplicacoes/ATBHE/ConsultaOptantes.app/";
    //private const String paginaValidacaoCNPJ = "ConsultarOpcao.aspx";
    private const String paginaPrincipalCNPJ = "ConsultarOpcao.aspx";
    private const String paginaCaptchaCNPJ = "http://www.receita.fazenda.gov.br/scripts/srf/intercepta/captcha.aspx?opt=image";


    public Bitmap GetCaptchaCNPJ()
    {
        String htmlResult;
        Bitmap retorno;

        using (var wc = new CookieAwareWebClient())
        {
            wc.SetCookieContainer(_cookies);
            wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; Synapse)";
            wc.Headers[HttpRequestHeader.KeepAlive] = "300";
            htmlResult = wc.DownloadString(urlBaseReceitaFederalCNPJ + paginaPrincipalCNPJ);
        }
        if (htmlResult.Length > 0)
        {
            var wc2 = new CookieAwareWebClient();
            wc2.SetCookieContainer(_cookies);
            wc2.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; Synapse)";
            wc2.Headers[HttpRequestHeader.KeepAlive] = "300";

            retorno = new System.Drawing.Bitmap(new System.IO.MemoryStream(wc2.DownloadData(paginaCaptchaCNPJ)));

            return retorno;

        }
        return null;
    }

    public String ConsultaCNPJ(string aCNPJ, string aCaptcha)
    {
        var request = (HttpWebRequest)WebRequest.Create(urlBaseReceitaFederalCNPJ + paginaPrincipalCNPJ);
        request.ProtocolVersion = HttpVersion.Version10;
        request.CookieContainer = _cookies;
        request.Method = "POST";



        string postData = "";
        postData = string.Format("{0}__EVENTTARGET={1}&", postData, "null");
        postData = string.Format("{0}__EVENTARGUMENT={1}&", postData, "null");
        postData = string.Format("{0}__VIEWSTATE={1}&", postData, "/wEPDwUKLTI4MDEzODYyMg9kFgJmD2QWAgIDD2QWAgICD2QWAgIDD2QWBAIBD2QWAgIDDw8WAh4RQ29udHJvbFRvVmFsaWRhdGUFEjYzNTg5NjgxMjAzOTYzMjk2MmRkAgMPDxYCHgdWaXNpYmxlaGQWCAITD2QWBAIDDzwrABEBARAWABYAFgBkAgcPPCsAEQEBEBYAFgAWAGQCGQ9kFgICAw88KwARAQEQFgAWABYAZAIfD2QWAgIDDzwrABEBARAWABYAFgBkAiUPZBYCAgMPPCsAEQEBEBYAFgAWAGQYBQU7Y3RsMDAkQ29udGVudFBsYWNlSG9sZGVyQ29udGV1ZG8kZ3JkUGVyaW9kb3NBbnRlcmlvcmVzU0lNRUkPZ2QFOmN0bDAwJENvbnRlbnRQbGFjZUhvbGRlckNvbnRldWRvJGdyZEFnZW5kYW1lbnRvc09wY2FvU2luYWMPZ2QFOWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlckNvbnRldWRvJEdyaWRWaWV3T3Bjb2VzQW50ZXJpb3Jlcw9nZAUyY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyQ29udGV1ZG8kZ3JkRXZlbnRvc0Z1dHVyb3MPZ2QFN2N0bDAwJENvbnRlbnRQbGFjZUhvbGRlckNvbnRldWRvJGdyZEV2ZW50b3NGdXR1cm9zU2ltZWkPZ2RVew7KgLxLzgzTZSEUz2W7HMrmi/hfWwILEBUqE+RWxg==");
        postData = string.Format("{0}__EVENTVALIDATION={1}&", postData, "/wEWBgLq/eloAqye/fEBAubugugBArTc7JALAoj16KgKAsPCk6ABUmPOXmbrPKdGe79cSZ5xd7+tpvEhZPqniHtlSzKz9g0=");
        postData = string.Format("{0}ctl00$ContentPlaceHolderConteudo$635896812039632962={1}&", postData, new Regex(@"[^\d]").Replace(aCNPJ, string.Empty));
        postData = string.Format("{0}ctl00$ContentPlaceHolderConteudo$HiddenField1={1}&", postData, "635896812039632962");
        postData = string.Format("{0}ctl00$ContentPlaceHolderConteudo$hddServidorCaptcha={1}&", postData, "pro");
        postData = string.Format("{0}ctl00$ContentPlaceHolderConteudo$txtTexto_captcha_serpro_gov_br={1}&", postData, aCaptcha);
        postData = string.Format("{0}ctl00$ContentPlaceHolderConteudo$btnConfirmar={1}&", postData, "Consultar");


        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse response = request.GetResponse();
        StreamReader stHtml = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"));
        String retorno = stHtml.ReadToEnd();

        if (retorno.Contains("Verifique se o mesmo foi digitado corretamente"))
            throw new System.InvalidOperationException("O número do CNPJ não foi digitado corretamente");
        if (retorno.Contains("Erro na Consulta"))
            throw new System.InvalidOperationException("Os caracteres digitados não correspondem com a imagem");
        if (retorno.Contains("Esta página tem como objetivo permitir"))
            throw new System.InvalidOperationException("Erro não mapeado...");

        return retorno;
    }
}

Has anyone else already implemented or can you help me implement this type of Query?

    
asked by anonymous 29.01.2016 / 20:17

1 answer

6

After much breaking the head and getting help from a colleague of profession in the WEB area I was able to reformulate the methods and get the desired result ...

Below detailed and functional code ...! PS: I was failing to send the parameters and also in the cookie pass containing the TOKEN from Captcha ...
That serves as an example for many others who needed and gave up on the way ...

public class ConsultaCNPJSimplesNacional
{
    private string viewState, eventValidation, hiddenField1, servidorCaptcha, stringCookies, token = string.Empty, img = string.Empty;

    private const String urlBaseReceitaFederalCNPJ = "http://www8.receita.fazenda.gov.br/SimplesNacional/Aplicacoes/ATBHE/ConsultaOptantes.app/ConsultarOpcao.aspx";
    private const String paginaCaptchaCNPJ = "http://www8.receita.fazenda.gov.br/SimplesNacional/Aplicacoes/ATBHE/ConsultaOptantes.app/Captcha/Inicializa.ashx";

    /// <summary>
    /// Captura Captcha e também armazena valores de alguns parametros a serem enviados na Consulta
    /// </summary>
    /// <returns>Retorna BITMAP contendo imagem Captcha</returns>
    public Bitmap GetCaptchaCNPJ()
    {
        String htmlResult = string.Empty;
        Bitmap retorno = null;

        try
        {
            WebClient wc = new WebClient();

            wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            //requisita valores das variaveis para armazenamento
            htmlResult = wc.DownloadString(urlBaseReceitaFederalCNPJ);

            if (htmlResult.Length > 0)
            {
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlResult);


                servidorCaptcha = doc.DocumentNode.Descendants("input")
                                .First(i => i.Attributes["id"] != null &&
                                            i.Attributes["id"].Value == "hddServidorCaptcha")
                                .Attributes["value"].Value;

                viewState = doc.DocumentNode.Descendants("input")
                                .First(i => i.Attributes["id"] != null &&
                                            i.Attributes["id"].Value == "__VIEWSTATE")
                                .Attributes["value"].Value;

                eventValidation = doc.DocumentNode.Descendants("input")
                                .First(i => i.Attributes["id"] != null &&
                                            i.Attributes["id"].Value == "__EVENTVALIDATION")
                                .Attributes["value"].Value;

                hiddenField1 = doc.DocumentNode.Descendants("input")
                                .First(i => i.Attributes["id"] != null &&
                                            i.Attributes["id"].Value == "ctl00_ContentPlaceHolderConteudo_HiddenField1")
                                .Attributes["value"].Value;

                //requisita Captcha armazenando TOKEN a ser utilizado na Consulta
                htmlResult = wc.DownloadString(paginaCaptchaCNPJ);

                if (htmlResult.Length > 0)
                {
                    stringCookies = htmlResult;

                    string[] x = Regex.Split(htmlResult, "\",\"");

                    foreach (string s in x)
                    {
                        if (s.IndexOf("Token") >= 0)
                            token = new Regex(@"[^\d]").Replace(s, string.Empty);
                        else if (s.IndexOf("Dados") >= 0)
                            img = s.Substring(8);
                    }

                    if (img.Length > 0)
                    {
                        byte[] imageBytes = Convert.FromBase64String(img);
                        MemoryStream ms = new MemoryStream(imageBytes);

                        Image image = Image.FromStream(ms, true, true);

                        retorno = (Bitmap)image;
                    }
                }
            }

        }
        catch (Exception)
        {
            throw;
        }

        return retorno;

    }

    /// <summary>
    /// Faz Consulta do CNPJ informado pelo Usuário, verificando se o mesmo é Optante pelo Simples Nacional
    /// </summary>
    /// <param name="aCNPJ">CNPJ sem traços</param>
    /// <param name="aCaptcha">Conteúdo Captcha informado</param>
    /// <returns>Retorna Dados Cadastrais CNPJ e Regime Tributário Optado</returns>
    public List<KeyValuePair<string, string>> ConsultaCNPJ(string aCNPJ, string aCaptcha)
    {
        try
        {
            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                wc.Headers[HttpRequestHeader.Cookie] = "captcha_token=" + token;
                wc.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                wc.Headers[HttpRequestHeader.AcceptLanguage] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4";
                wc.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
                wc.Headers[HttpRequestHeader.Host] = "www8.receita.fazenda.gov.br";
                wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36";


                byte[] response =
                wc.UploadValues(urlBaseReceitaFederalCNPJ, new NameValueCollection()
                {
                    { "__EVENTTARGET", "null" }, //conteudo estático, sempre nulo
                    { "__EVENTARGUMENT", "null" }, //conteudo estático, sempre nulo
                    { "__VIEWSTATE", viewState }, //conteudo armazenado durante consulta Captcha
                    { "__EVENTVALIDATION", eventValidation },//conteudo armazenado durante consulta Captcha
                    { string.Format("ctl00$ContentPlaceHolderConteudo${0}", hiddenField1), new Regex(@"[^\d]").Replace(aCNPJ, string.Empty) }, //identifica parametro CNPJ adicionando à propriedade NAME o conteudo dinâmico da varíavel armazenada durante captura Captcha
                    { "ctl00$ContentPlaceHolderConteudo$HiddenField1", hiddenField1 }, //conteudo armazenado durante consulta Captcha
                    { "ctl00$ContentPlaceHolderConteudo$hddServidorCaptcha", servidorCaptcha }, //conteudo armazenado durante consulta Captcha
                    { "ctl00$ContentPlaceHolderConteudo$txtTexto_captcha_serpro_gov_br", aCaptcha }, //Captcha digitado
                    { "ctl00$ContentPlaceHolderConteudo$btnConfirmar", "Consultar" } //conteúdo estático de consulta
                });

                string result = System.Text.Encoding.UTF8.GetString(response);

                //trata e retorna valores encontrados
                return TratarRetorno(result);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Captura os dados de Retorno do Site da Receita Federal
    /// </summary>
    /// <param name="resultado">STRING contendo Retorno da Consulta Realizada na Receita Federal</param>
    /// <returns>Retorna lista contendo Dados Cadastrais e Situação do CNPJ na Receita Federal</returns>
    private static List<KeyValuePair<string, string>> TratarRetorno(string resultado)
    {
        List<KeyValuePair<string, string>> retorno = new List<KeyValuePair<string, string>>();

        try
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(resultado);

            #region ... Validação CAPTCHA ERROR...
            var itemError = (HtmlAgilityPack.HtmlNodeCollection)doc.DocumentNode.SelectNodes("//div[@id='ctl00_ContentPlaceHolderConteudo_pnlConsulta']");

            if (itemError != null)
            {
                foreach (HtmlAgilityPack.HtmlNode node in itemError)
                {
                    if (node.SelectNodes("//span[@id='ctl00_ContentPlaceHolderConteudo_lblErroCaptcha']") != null)
                        throw new Exception("Caracteres Captcha Inválidos.");
                }
            }
            #endregion

            #region ...Dados Empresa...
            var dadosEmpresa = (HtmlAgilityPack.HtmlNodeCollection)doc.DocumentNode.SelectNodes("//div[@id='ctl00_ContentPlaceHolderConteudo_Panel1']");

            if (dadosEmpresa != null)
            {
                foreach (HtmlAgilityPack.HtmlNode node in dadosEmpresa)
                {
                    foreach (HtmlAgilityPack.HtmlNode node2 in node.SelectNodes("//span[@id]"))
                    {
                        string attributeValue = node2.GetAttributeValue("id", "");
                        if (attributeValue == "ctl00_ContentPlaceHolderConteudo_lblCNPJ")
                        {
                            retorno.Add(new KeyValuePair<string, string>("CNPJ", node2.InnerText));
                        }
                        else if (attributeValue == "ctl00_ContentPlaceHolderConteudo_lblNomeEmpresa")
                        {
                            retorno.Add(new KeyValuePair<string, string>("EMPRESA", node2.InnerText));
                        }
                    }
                }

            }
            #endregion

            #region ...Situação do CNPJ...
            var dadosRetorno = (HtmlAgilityPack.HtmlNodeCollection)doc.DocumentNode.SelectNodes("//div[@id='ctl00_ContentPlaceHolderConteudo_Panel2']");

            if (dadosRetorno != null)
            {
                foreach (HtmlAgilityPack.HtmlNode node in dadosRetorno)
                {
                    foreach (HtmlAgilityPack.HtmlNode node2 in node.SelectNodes("//span[@id]"))
                    {
                        string attributeValue = node2.GetAttributeValue("id", "");
                        if (attributeValue == "ctl00_ContentPlaceHolderConteudo_lblSituacaoSimples")
                        {
                            retorno.Add(new KeyValuePair<string, string>("SITUAÇÃO", node2.InnerText));
                        }
                    }
                }
            }
            #endregion
        }
        catch (Exception)
        {
            throw;
        }

        return retorno;
    }
}
    
01.02.2016 / 16:14