I am accessing the link from the Fazenda website ( link ) in order to return the filled NFe.
When I make the first request I get the Captcha but when I send the Captcha answer it always returns the error "The session has expired. Please consult again.".
I have tried to put Cookie as static but it did not work well ...
The CNPJ query that looks good works with NFe not!
Note: I can not use the WebService because of the date range limitation.
Here is the code for 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