Ejemplo n.º 1
0
 public string ComposePost(PostItems itemsForPost)
 {
     var outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
     outgoingQueryString.Add("__EVENTTARGET", itemsForPost.EventTarget);
     outgoingQueryString.Add("__EVENTARGUMENT", itemsForPost.EventArgument);
     outgoingQueryString.Add("__VIEWSTATE", itemsForPost.ViewState);
     outgoingQueryString.Add("__VIEWSTATEGENERATOR", itemsForPost.ViewStateGenerator);
     outgoingQueryString.Add("__EVENTVALIDATION", itemsForPost.EventValidation);
     outgoingQueryString.Add("ctl00$txtPalavraChave", itemsForPost.PalavraChave);
     outgoingQueryString.Add("ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta", itemsForPost.ChaveAcessoCompleta);
     outgoingQueryString.Add("ctl00$ContentPlaceHolder1$txtCaptcha", itemsForPost.Captcha);
     outgoingQueryString.Add("ctl00$ContentPlaceHolder1$btnConsultar", itemsForPost.BtnConsultar);
     outgoingQueryString.Add("ctl00$ContentPlaceHolder1$token", itemsForPost.Token);
     outgoingQueryString.Add("ctl00$ContentPlaceHolder1$captchaSom", itemsForPost.CaptchaSom);
     outgoingQueryString.Add("hiddenInputToUpdateATBuffer_CommonToolkitScripts", itemsForPost.CommomToolkitScripts);
     return outgoingQueryString.ToString();
 }
Ejemplo n.º 2
0
        public string ComposePost(PostItems itemsForPost)
        {
            var outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);

            outgoingQueryString.Add("__EVENTTARGET", itemsForPost.EventTarget);
            outgoingQueryString.Add("__EVENTARGUMENT", itemsForPost.EventArgument);
            outgoingQueryString.Add("__VIEWSTATE", itemsForPost.ViewState);
            outgoingQueryString.Add("__VIEWSTATEGENERATOR", itemsForPost.ViewStateGenerator);
            outgoingQueryString.Add("__EVENTVALIDATION", itemsForPost.EventValidation);
            outgoingQueryString.Add("ctl00$txtPalavraChave", itemsForPost.PalavraChave);
            outgoingQueryString.Add("ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta", itemsForPost.ChaveAcessoCompleta);
            outgoingQueryString.Add("ctl00$ContentPlaceHolder1$txtCaptcha", itemsForPost.Captcha);
            outgoingQueryString.Add("ctl00$ContentPlaceHolder1$btnConsultar", itemsForPost.BtnConsultar);
            outgoingQueryString.Add("ctl00$ContentPlaceHolder1$token", itemsForPost.Token);
            outgoingQueryString.Add("ctl00$ContentPlaceHolder1$captchaSom", itemsForPost.CaptchaSom);
            outgoingQueryString.Add("hiddenInputToUpdateATBuffer_CommonToolkitScripts", itemsForPost.CommomToolkitScripts);
            return(outgoingQueryString.ToString());
        }
Ejemplo n.º 3
0
        public void ValidatePost(PostItems itemsForPost)
        {
            if (!string.IsNullOrWhiteSpace(itemsForPost.EventTarget))
            {
                throw new InvalidOperationException("__EVENTTARGET deve ser vazio!");
            }

            if (!string.IsNullOrWhiteSpace(itemsForPost.EventArgument))
            {
                throw new InvalidOperationException("__EVENTARGUMENT deve ser vazio!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.ViewState))
            {
                throw new InvalidOperationException("__VIEWSTATE deve possuir um valor!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.EventValidation))
            {
                throw new InvalidOperationException("__EVENTVALIDATION deve possuir um valor!");
            }

            if (!string.IsNullOrWhiteSpace(itemsForPost.PalavraChave))
            {
                throw new InvalidOperationException("ctl00$txtPalavraChave deve ser vazia!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.ChaveAcessoCompleta))
            {
                throw new InvalidOperationException("A chave de acesso não foi informada!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.Captcha))
            {
                throw new InvalidOperationException("O captcha não foi informado!");
            }

            if (itemsForPost.CommomToolkitScripts != "1")
            {
                throw new InvalidOperationException("hiddenInputToUpdateATBuffer_CommonToolkitScripts possui um valor inválido.");
            }
        }
Ejemplo n.º 4
0
        public void ValidatePost(PostItems itemsForPost)
        {
            if (!string.IsNullOrWhiteSpace(itemsForPost.EventTarget))
            {
                throw new InvalidOperationException("__EVENTTARGET deve ser vazio!");
            }

            if (!string.IsNullOrWhiteSpace(itemsForPost.EventArgument))
            {
                throw new InvalidOperationException("__EVENTARGUMENT deve ser vazio!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.ViewState))
            {
                throw new InvalidOperationException("__VIEWSTATE deve possuir um valor!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.EventValidation))
            {
                throw new InvalidOperationException("__EVENTVALIDATION deve possuir um valor!");
            }

            if (!string.IsNullOrWhiteSpace(itemsForPost.PalavraChave))
            {
                throw new InvalidOperationException("ctl00$txtPalavraChave deve ser vazia!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.ChaveAcessoCompleta))
            {
                throw new InvalidOperationException("A chave de acesso não foi informada!");
            }

            if (string.IsNullOrWhiteSpace(itemsForPost.Captcha))
            {
                throw new InvalidOperationException("O captcha não foi informado!");
            }

            if (itemsForPost.CommomToolkitScripts != "1")
            {
                throw new InvalidOperationException("hiddenInputToUpdateATBuffer_CommonToolkitScripts possui um valor inválido.");
            }
        }
Ejemplo n.º 5
0
        public DownloadedHtmlData Post(PostItems itemsForPost)
        {
            ValidatePost(itemsForPost);

            var postData = ComposePost(itemsForPost);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            var request = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=");
            request.CookieContainer = sessionCookie;
            request.UserAgent = UserAgent;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            var dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            var pageResponse = request.GetResponse();

            var docUserWebPage = new HtmlDocument();
            docUserWebPage.Load(pageResponse.GetResponseStream());
            var documentText = docUserWebPage.DocumentNode.InnerHtml;
            if (documentText.Contains("Código da Imagem inválido."))
            {
                throw new InvalidDataException("Código da Imagem inválido. Tente novamente.");
            }

            if (documentText.Contains("NF-e INEXISTENTE na base nacional"))
            {
                throw new InvalidDataException("NF-e INEXISTENTE na base nacional.");
            }

            var printRequest = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consultaImpressao.aspx?tipoConsulta=completa");
            printRequest.CookieContainer = sessionCookie;
            printRequest.UserAgent = UserAgent;
            printRequest.ContentType = "application/x-www-form-urlencoded";
            var printResponse = printRequest.GetResponse();
            var printResponseStream = printResponse.GetResponseStream();

            var printUserPage = new HtmlDocument();
            printUserPage.Load(printResponseStream);

            var result = new DownloadedHtmlData();
            var spanChaveAcesso = printUserPage.GetElementbyId("lblChaveAcesso");
            Check(spanChaveAcesso);
            result.ChaveAcessso = spanChaveAcesso.InnerText.Trim();
            result.DadosNfe = GetDataItems(printUserPage, "NFe");
            var operationScience = printUserPage.DocumentNode.Descendants().Where(e => e.Id.Contains("CienciaOperacao")).ToList();
            var scienceOperationsList = operationScience.Select(science => GetDataItems(printUserPage, science.Id)).ToList();
            result.ScienceOperations = scienceOperationsList;
            result.DadosEmitente = GetDataItems(printUserPage, "Emitente");
            result.DadosDestinatario = GetDataItems(printUserPage, "DestRem");
            result.Products = GetProducts(printUserPage);
            result.Totais = GetDataItems(printUserPage, "Totais");
            result.DadosTransporte = GetDataItems(printUserPage, "Transporte");
            result.DadosCobranca = GetDataItems(printUserPage, "Cobranca");
            result.InformacoesAdicionais = GetDataItems(printUserPage, "Inf");
            result.NotaFiscalAvulsa = GetDataItems(printUserPage, "Avulsa");

            return result;
        }
Ejemplo n.º 6
0
        public PostItems GetItemsForPost()
        {
            ////Get items for post
            var request = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=");
            sessionCookie = new CookieContainer();
            request.CookieContainer = sessionCookie;
            request.UserAgent = UserAgent;
            var response = request.GetResponse();
            var responseStream = response.GetResponseStream();

            var doc = new HtmlDocument();
            doc.Load(responseStream);

            var itemsForPost = new PostItems
            {
                EventTarget = string.Empty,
                EventArgument = string.Empty,
                PalavraChave = string.Empty
            };

            var formElement = doc.GetElementbyId("aspnetForm");
            if (formElement != null)
            {
                itemsForPost.FormAction = formElement.GetAttributeValue("action", "nodata");
            }
            else
            {
                throw new InvalidOperationException("Resposta sem um formulário web.");
            }

            var capcthElement = doc.GetElementbyId("ContentPlaceHolder1_imgCaptcha");
            if (capcthElement != null)
                itemsForPost.Base64Image = capcthElement.GetAttributeValue("src", "nosrc");

            var viewStateElement = doc.GetElementbyId("__VIEWSTATE");
            if (viewStateElement != null)
                itemsForPost.ViewState = viewStateElement.GetAttributeValue("value", "novalue");

            var viewStateGeneratorElement = doc.GetElementbyId("__VIEWSTATEGENERATOR");
            if (viewStateGeneratorElement != null)
                itemsForPost.ViewStateGenerator = viewStateGeneratorElement.GetAttributeValue("value", "novalue");

            var eventValidationElement = doc.GetElementbyId("__EVENTVALIDATION");
            if (eventValidationElement != null)
                itemsForPost.EventValidation = eventValidationElement.GetAttributeValue("value", "novalue");

            itemsForPost.BtnConsultar = "Continuar";

            var eventTokenElement = doc.GetElementbyId("ContentPlaceHolder1_token");
            if (eventTokenElement != null)
                itemsForPost.Token = eventTokenElement.GetAttributeValue("value", "novalue");

            var captchaSomElement = doc.GetElementbyId("ContentPlaceHolder1_captchaSom");
            if (captchaSomElement != null)
                itemsForPost.CaptchaSom = captchaSomElement.GetAttributeValue("value", "novalue");

            itemsForPost.CommomToolkitScripts = "1";

            return itemsForPost;
        }
Ejemplo n.º 7
0
        public PostItems GetItemsForPost()
        {
            ////Get items for post
            var request = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=");

            sessionCookie           = new CookieContainer();
            request.CookieContainer = sessionCookie;
            request.UserAgent       = UserAgent;
            var response       = request.GetResponse();
            var responseStream = response.GetResponseStream();

            var doc = new HtmlDocument();

            doc.Load(responseStream);

            var itemsForPost = new PostItems
            {
                EventTarget   = string.Empty,
                EventArgument = string.Empty,
                PalavraChave  = string.Empty
            };

            var formElement = doc.GetElementbyId("aspnetForm");

            if (formElement != null)
            {
                itemsForPost.FormAction = formElement.GetAttributeValue("action", "nodata");
            }
            else
            {
                throw new InvalidOperationException("Resposta sem um formulário web.");
            }

            var capcthElement = doc.GetElementbyId("ContentPlaceHolder1_imgCaptcha");

            if (capcthElement != null)
            {
                itemsForPost.Base64Image = capcthElement.GetAttributeValue("src", "nosrc");
            }

            var viewStateElement = doc.GetElementbyId("__VIEWSTATE");

            if (viewStateElement != null)
            {
                itemsForPost.ViewState = viewStateElement.GetAttributeValue("value", "novalue");
            }


            var viewStateGeneratorElement = doc.GetElementbyId("__VIEWSTATEGENERATOR");

            if (viewStateGeneratorElement != null)
            {
                itemsForPost.ViewStateGenerator = viewStateGeneratorElement.GetAttributeValue("value", "novalue");
            }


            var eventValidationElement = doc.GetElementbyId("__EVENTVALIDATION");

            if (eventValidationElement != null)
            {
                itemsForPost.EventValidation = eventValidationElement.GetAttributeValue("value", "novalue");
            }

            itemsForPost.BtnConsultar = "Continuar";

            var eventTokenElement = doc.GetElementbyId("ContentPlaceHolder1_token");

            if (eventTokenElement != null)
            {
                itemsForPost.Token = eventTokenElement.GetAttributeValue("value", "novalue");
            }

            var captchaSomElement = doc.GetElementbyId("ContentPlaceHolder1_captchaSom");

            if (captchaSomElement != null)
            {
                itemsForPost.CaptchaSom = captchaSomElement.GetAttributeValue("value", "novalue");
            }

            itemsForPost.CommomToolkitScripts = "1";

            return(itemsForPost);
        }
Ejemplo n.º 8
0
        public DownloadedHtmlData Post(PostItems itemsForPost)
        {
            ValidatePost(itemsForPost);

            var postData = ComposePost(itemsForPost);

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            var request = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=");

            request.CookieContainer = sessionCookie;
            request.UserAgent       = UserAgent;
            request.Method          = "POST";
            request.ContentType     = "application/x-www-form-urlencoded";
            request.ContentLength   = byteArray.Length;
            var dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            var pageResponse = request.GetResponse();

            var docUserWebPage = new HtmlDocument();

            docUserWebPage.Load(pageResponse.GetResponseStream());
            var documentText = docUserWebPage.DocumentNode.InnerHtml;

            if (documentText.Contains("Código da Imagem inválido."))
            {
                throw new InvalidDataException("Código da Imagem inválido. Tente novamente.");
            }

            if (documentText.Contains("NF-e INEXISTENTE na base nacional"))
            {
                throw new InvalidDataException("NF-e INEXISTENTE na base nacional.");
            }

            var printRequest = (HttpWebRequest)WebRequest.Create("http://www.nfe.fazenda.gov.br/portal/consultaImpressao.aspx?tipoConsulta=completa");

            printRequest.CookieContainer = sessionCookie;
            printRequest.UserAgent       = UserAgent;
            printRequest.ContentType     = "application/x-www-form-urlencoded";
            var printResponse       = printRequest.GetResponse();
            var printResponseStream = printResponse.GetResponseStream();

            var printUserPage = new HtmlDocument();

            printUserPage.Load(printResponseStream);

            var result          = new DownloadedHtmlData();
            var spanChaveAcesso = printUserPage.GetElementbyId("lblChaveAcesso");

            Check(spanChaveAcesso);
            result.ChaveAcessso = spanChaveAcesso.InnerText.Trim();
            result.DadosNfe     = GetDataItems(printUserPage, "NFe");
            var operationScience      = printUserPage.DocumentNode.Descendants().Where(e => e.Id.Contains("CienciaOperacao")).ToList();
            var scienceOperationsList = operationScience.Select(science => GetDataItems(printUserPage, science.Id)).ToList();

            result.ScienceOperations     = scienceOperationsList;
            result.DadosEmitente         = GetDataItems(printUserPage, "Emitente");
            result.DadosDestinatario     = GetDataItems(printUserPage, "DestRem");
            result.Products              = GetProducts(printUserPage);
            result.Totais                = GetDataItems(printUserPage, "Totais");
            result.DadosTransporte       = GetDataItems(printUserPage, "Transporte");
            result.DadosCobranca         = GetDataItems(printUserPage, "Cobranca");
            result.InformacoesAdicionais = GetDataItems(printUserPage, "Inf");
            result.NotaFiscalAvulsa      = GetDataItems(printUserPage, "Avulsa");

            return(result);
        }
Ejemplo n.º 9
0
 private void UpdateForm(PostItems itemsForPost)
 {
     var normalizedBase64Img = itemsForPost.Base64Image.Replace("data:image/png;base64,", string.Empty);
     captchaPictureBox.Image = Helpers.Base64StringToBitmap(normalizedBase64Img);
 }
Ejemplo n.º 10
0
 private void InitializeForPost()
 {
     postItems = helper.GetItemsForPost();
     UpdateForm(postItems);
 }