I am researching to develop an invoice query facilitator from the NFe Farm website so that the recipient can consult and download it.
I have created a GET
request by bringing captcha to use the site's own security validation, and then I make a POST
with the% in> captcha at the end returns the answer:
Unexpected failure. The system may be momentarily unavailable or a timely error has occurred.
Code
public void CarregaCaptcha()
{
TempData[CookieTemp] = "";
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(UrlBase + UrlGet);
httpWebRequest.CookieContainer = cookieJar;
var response = httpWebRequest.GetResponse();
foreach (Cookie c in cookieJar.GetCookies(httpWebRequest.RequestUri))
{
TempData["CookieName"] = c.Name;
TempData["CookieValue"] = c.Value;
}
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = WebRequestMethods.Http.Get; //"GET";
httpWebRequest.AllowAutoRedirect = false;
StreamReader stHtml = new StreamReader(httpWebRequest.GetResponse().GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"));
HtmlResponse = stHtml.ReadToEnd();
stHtml.Close();
UrlImagemCaptcha = GetValue(HtmlResponse, StrImagemCaptcha);
if (UrlImagemCaptcha.Length > 0)
ViewBag.Captcha = UrlImagemCaptcha;
ViewBag.Responde = HtmlResponse;
}
}
public void ConsultaNfe(string chave, string captcha)
{
// Montando a Lista de Parametros para o post
var parametros = "__EVENTTARGET=&" +
"__EVENTARGUMENT=&" +
"__VIEWSTATE=" + viewState + "&" +
"__VIEWSTATEGENERATOR" + viewGenerator + "&" +
"__EVENTVALIDATION=" + Validacao + "&" +
"ctl00_txtPalavraChave=&" +
"ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta=" + chave + "&" +
"ctl00$ContentPlaceHolder1$txtCaptcha=" + captcha + "&" +
"ctl00$ContentPlaceHolder1$btnConsultar=Continuar" +
"ctl00$ContentPlaceHolder1$token=" + token + "&"+
"ctl00$ContentPlaceHolder1$captchaSom="+ captchaSom + "&"+
"hiddenInputToUpdateATBuffer_CommonToolkitScripts=1";
MyHttpPost(parametros);
}**strong text**
}
public void MyHttpPost(String parametros)
{
var request = (HttpWebRequest)WebRequest.Create(UrlBase + UrlPost);
request.ProtocolVersion = HttpVersion.Version10;
Uri target = new Uri(UrlBase);
cookieContainer.Add(new Cookie(TempData["CookieName"].ToString(), TempData["CookieValue"].ToString()) { Domain = target.Host });
request.CookieContainer = cookieContainer;
request.Method = "POST";
request.Timeout = 10000;
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
byte[] byteArray = Encoding.UTF8.GetBytes(parametros);
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"));
var resposta = stHtml.ReadToEnd();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}enter code here
}