public string ConsumirValidador(DatosParaValidar datos)
        {
            //string apiURL = "http://webapi.factura-electronica-gratis.net.100-42-52-208.hgws28.hgwin.temp.domains/API/ValidateCfdi";
            string apiURL = "http://localhost:61136/API/ValidateCfdi";
            string answer = "";

            using (WebClient client = new WebClient())
            {
                //Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
                NameValueCollection myNameValueCollection = new NameValueCollection();

                // Add necessary parameter/value pairs to the name/value container.
                myNameValueCollection.Add("data", datos.JsonDataToString());
                myNameValueCollection.Add("RfcDistribuidor", datos.RfcDistribuidor);
                myNameValueCollection.Add("IdDistribuidor", datos.IdDistribuidor);

                byte[] responsebytes = null;

                try
                {
                    responsebytes = client.UploadValues(apiURL, "POST", myNameValueCollection);
                    answer        = Encoding.UTF8.GetString(responsebytes);
                }
                catch (Exception fault)
                {
                    answer = "Ultima ejecución: " + DateTime.Now + "ocurrió un error al procesar la solicitud: " + fault.Message + " Detalles: " + fault.StackTrace + answer;
                }
            }
            return(answer);
        }
Exemple #2
0
        protected void BtnValidate_Click(object sender, EventArgs e)
        {
            //if (FplNewXml.HasFile)
            //{
            string filename       = "";
            String asBase64String = "";

            LblErrorMessage.Text = "";



            try
            {
                if (FplNewXml.HasFile)
                {
                    //filename = Guid.NewGuid().ToString("N") + Path.GetFileName(FplNewXml.FileName);
                    filename = Path.GetFileName(FplNewXml.FileName);

                    //FplNewXml.SaveAs(Server.MapPath("~\\Ejemplos\\") + filename);

                    //reading the information

                    //byte[] AsBytes = File.ReadAllBytes(Server.MapPath("~\\Ejemplos\\") + filename);
                    byte[] asBytes = FplNewXml.FileBytes;
                    asBase64String = Convert.ToBase64String(asBytes);

                    //deleting file
                    //File.Delete(Server.MapPath("~\\Ejemplos\\") + filename);
                }
                else
                {
                    //no se manda ningún archivo
                    asBase64String = null;
                    filename       = "";
                }
            }
            catch (Exception ex)
            {
                LblErrorMessage.Text = "Por favor elija un archivo.";
                // TbxResult.Text = " Ejecución sin archivo adjunto, elija un archivo. \r\n" + TbxResult.Text;
                LblResult.Text = " Ejecución sin archivo adjunto, elija un archivo. \r\n" + LblResult.Text;
                return;
            }



            DatosParaValidar validaMe = new DatosParaValidar();

            validaMe.RfcDistribuidor = TbxRfcDistribuidor.Text;
            validaMe.IdDistribuidor  = TbxIdDistribuidor.Text;

            validaMe.Version              = TbxVersion.Text;
            validaMe.Xml                  = asBase64String;
            validaMe.FileName             = filename;
            validaMe.EnterpriseRfc        = TbxEnterpriseRfc.Text;
            validaMe.Identar              = CbxIdent.Checked;
            validaMe.ReportePdf           = CbxReportPdf.Checked;
            validaMe.AmountEpsilon        = TbxAmountEpsilon.Text;
            validaMe.DocumentoStamp       = TbxDocumentToStamp.Text;
            validaMe.RfcEmpresa           = TbxRfcEmpresa.Text;
            validaMe.SupportFormatsOnDate = CbxSupportFormatsOnDate.Checked;
            // validaMe.ValidarSepomex = CbxValidSepomex.Checked;
            validaMe.ValidarSchema = CbxValidSchema.Checked;
            // validaMe.ValidarCsd = CbxValidCsd.Checked;
            // validaMe.ValidarLco = CbxVAlidLco.Checked;

            if (CbxRequestDocument.Checked)
            {
                validaMe.Documento = new DPVDocumento();
            }
            else
            {
                validaMe.Documento = null;
            }

            if (CbxRequestCertificate.Checked)
            {
                validaMe.Certificado = new DPVCertificado();
            }
            else
            {
                validaMe.Certificado = null;
            }

            if (CbxRequestTimbre.Checked)
            {
                validaMe.Timbre = new DPVTimbre();
            }
            else
            {
                validaMe.Timbre = null;
            }

            ConsumidorValidador consumidorRest = new ConsumidorValidador();

            //Task<string> answer = null;
            //answer = consumidorRest.ConsumirValidador(validaMe);
            string answerString = consumidorRest.ConsumirValidador(validaMe);



            // TbxResult.Text = answer.Result;
            // TbxResult.Text = answerString;
            LblResult.Text = answerString.Replace(" ", "&nbsp;").Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br/>");

            JToken token = null;

            try
            {
                JObject jsonObject = JObject.Parse(answerString);
                token = jsonObject.GetValue("ReportePdf");
            }
            catch (Exception fault)
            {
                // TbxResult.Text = TbxResult.Text + "\r\nEl valor devuelto no se puede interpretar, no es un JSON ";
                LblResult.Text = LblResult.Text + "\r\nEl valor devuelto no se puede interpretar, no es un JSON ";
            }

            if (token != null)
            {
                // JObject thisisme = null;
                LblPdfString.Text      = token.Value <string>();
                LnkDownloadPDF.Visible = true;
            }
            else
            {
                LblPdfString.Text      = "";
                LnkDownloadPDF.Visible = false;
            }

            //Response.Clear();
            //Response.ContentType = "application/pdf";
            //Response.AddHeader("Content-Disposition", "attachment;filename=\"NuevoArchivoDeValidacion.pdf\"");

            //// edit this line to display ion browser and change the file name
            //Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));

            //// gets our pdf as a byte array and then sends it to the buffer

            //Response.Flush();

            //}
            //else
            //{
            //    LblErrorMessage.Text = "Por favor elija un archivo.";
            //    TbxResult.Text = "Ejecución sin archivo adjunto, elija un archivo.--------------------\r\n" + TbxResult.Text ;
            //}
        }