3
I’m accessing the link of the Farm site ( http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8%3d) with the aim of returning the filled Nfe.
When I make the first request I can bring the Captcha but when sending the Captcha reply always returns the error "The session has expired. Make the query again.".
I tried to put Cookie as Static but it didn’t work out tbm...
The CNPJ query that is very similar works but with Nfe does not!
Note: I cannot use the Webservice due to date period limitation.
Follow the code of my Query methods:
public String Consulta(String aNFE, String aCaptcha)
{
_hasError = false;
_msgError = String.Empty;
String retorno = String.Empty;
var request = (HttpWebRequest)WebRequest.Create(urlBaseReceitaFederal + paginaValidacao);
request.ProtocolVersion = HttpVersion.Version10;
request.CookieContainer = _cookies;
request.Method = "POST";
request.Timeout = 10000;
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
String viewState = PreencherVariavel("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"", "\" /></div>");
String eventValidate = PreencherVariavel("<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"", "\" /></div> <script");
String Token = PreencherVariavel("<input type=\"hidden\" name=\"ctl00$ContentPlaceHolder1$token\" id=\"ctl00_ContentPlaceHolder1_token\" value=\"", "\" /> <input type=\"hidden\" name=\"ctl00$ContentPlaceHolder1$captchaSom\"");
String CaptchaSom = PreencherVariavel("<input type=\"hidden\" name=\"ctl00$ContentPlaceHolder1$captchaSom\" id=\"ctl00_ContentPlaceHolder1_captchaSom\" value=\"", "\" /> <br /> <a id=\"ctl00_ContentPlaceHolder1_hlkAlterarCaptcha\"");
String viewStateGenerator = PreencherVariavel("<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"", "\" /> <input type=\"hidden\" name=\"__EVENTVALIDATION");
String postData = "";
postData = postData + "__EVENTTARGET=&";
postData = postData + "__EVENTARGUMENT=&";
postData = postData + "__VIEWSTATE=" + viewState + "&";
postData = postData + "__VIEWSTATEGENERATOR=" + viewStateGenerator + "&";
postData = postData + "__EVENTVALIDATION=" + eventValidate + "&";
postData = postData + "ctl00$ContentPlaceHolder1$PalavraChave=&";
postData = postData + "ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta=" + aNFE + "&";
postData = postData + "ctl00$ContentPlaceHolder1$txtCaptcha=" + aCaptcha + "&";
postData = postData + "ctl00$ContentPlaceHolder1$btnConsultar=Continuar&";
postData = postData + "ctl00$ContentPlaceHolder1$token=" + Token + "&";
postData = postData + "ctl00$ContentPlaceHolder1$captchaSom=" + CaptchaSom + "&";
postData = postData + "hiddenInputToUpdateATBuffer_CommonToolkitScripts=1";
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();
try
{
WebResponse response = request.GetResponse();
StreamReader stHtml = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"));
retorno = stHtml.ReadToEnd();
}
catch (Exception ex)
{
_hasError = true;
_msgError = ex.Message;
}
return retorno;
}
Att. PHMV
You are doing this wrong. The site has Captcha precisely to not exist queries via requisicoes - as you are doing. Know the official Farm web services and correctly implying.
– Thiago Lunardi
@Thiagolunardi It is not that he is doing 'wrong' maybe this is his only option since to use the above services is necessary a digital certificate.
– rubStackOverflow
Exactly, as I mentioned the Farm Web Service has limited dates so I can not use...
– PHMV
Okay, I may have really gone overboard. If you’re lucky, let me know. :)
– Thiago Lunardi