How to consult cnpj using Httpwebrequest?

Asked

Viewed 2,293 times

0

The thing is, I’m creating a program to perform CPF and CNPJ queries on the IRS website. It’s not an attempt to circumvent the system. I’m bringing the captcha to a Picturebox so that the user can just enter the characters without having to type in the cnpj or Cpf, which would be filled in automatically.

The part that performs the CPF query is simpler, I just filled in the http header fields manually by setting the session cookie obtained in the captcha request. The problem is in the CNPJ consultation.

The website of the IRS sends a request with the following header:

POST http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/valida.asp HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp
Accept-Language: pt-BR,pt;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 111
DNT: 1
Host: www.receita.fazenda.gov.br
Pragma: no-cache
Cookie: flag=1; ASPSESSIONIDAQDSBCCD=ENPHBBJDCNDBAFPALIEEGOPP; ASPSESSIONIDASDSBCCC=EJGNAFJDDFCIOBAHNIFCPEHC

After that he makes three other requests, with the CNPJ information consulted being brought after the third.

inserir a descrição da imagem aqui

What I’m not getting is to reproduce this query with an Httpwebrequest, even having set all the headers.

private void consultarPessoaJuridica()
    {
        string postContent = "origem=comprovante"
                           + "&cnpj=60872504000123"
                           + "&txtTexto_captcha_serpro_gov_br=" + textBoxCaptcha1.Text
                           + "&submit1=Consultar"
                           + "&search_type=cnpj";

        byte[] postBytesArray = Encoding.UTF8.GetBytes(postContent);

        WebRequest webRequest = WebRequest.Create("http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/valida.asp");
        ((HttpWebRequest)webRequest).MaximumAutomaticRedirections = 4;
        ((HttpWebRequest)webRequest).AllowAutoRedirect = true;
        ((HttpWebRequest)webRequest).Timeout = 10000;

        webRequest.Headers.Add("Pragma", "no-cache");
        ((HttpWebRequest)webRequest).Accept = "text/html, application/xhtml+xml, */*";
        webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
        webRequest.Headers.Add("Accept-Language", "pt-BR,pt;q=0.5");
        ((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
        webRequest.Headers.Add("Cookie", this.cookie + "; flag=1");
        webRequest.Headers.Add("DNT", "1");
        ((HttpWebRequest)webRequest).ContentLength = postBytesArray.Length;
        ((HttpWebRequest)webRequest).ContentType = "application/x-www-form-urlencoded";
        ((HttpWebRequest)webRequest).Referer = "http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp";
        ((HttpWebRequest)webRequest).KeepAlive = true;

        ((HttpWebRequest)webRequest).Host = "www.receita.fazenda.gov.br";
        webRequest.Method = "POST";

        // Get the request stream.
        using (Stream dataStream = webRequest.GetRequestStream())
        {
            // Write data to the request stream.
            dataStream.Write(postBytesArray, 0, postBytesArray.Length);
        }

        using (WebResponse webResponse = webRequest.GetResponse())
        using (Stream dataStream = webResponse.GetResponseStream())
        using (StreamReader reader = new StreamReader(dataStream))
        {
            string responseFromServer = reader.ReadToEnd();
        }

    }

This request appears on Fiddler as follows:

POST http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/valida.asp HTTP/1.1
Pragma: no-cache
Accept: text/html, application/xhtml+xml, */*
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Cookie: ASPSESSIONIDCQCSADDD=EGGPDBLDCBJEPHJJONMEHHCP; flag=1
DNT: 1
Content-Type: application/x-www-form-urlencoded
Referer: http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp
Host: www.receita.fazenda.gov.br
Content-Length: 111
Expect: 100-continue

inserir a descrição da imagem aqui

I believe this is happening because, for some reason, the status code obtained after the second request is 200, no other automatic redirect occurs after that.

  • 5

    Are you going to redistribute a resource from a website without his permission? for me this falls within the definition of the word "mock".

  • 1

    Wouldn’t be the first time someone did that to the recipe hahaa

  • @Eduardoalmeida in my case I just wanted to expedite the consultation without wasting too much time opening pages and filling out forms.

  • What @Hideki.Eduardo is trying to do is already widely used by a wide variety of applications. It has nothing to do with "scamming" is just a form of abstraction, I see no form of revenue loss since it does not gain anything with the amount of accesses and views.

1 answer

0


Try using System.Net.Webclient which is an abstraction of Httpwebrequest.

I recommend not using Allowautoredirect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.