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?