public string AutorizacionArchivos(string sClaveAcceso, string PathServer, string estadorecepcion) { string sResult = ""; StringBuilder sSoap = new StringBuilder(); XmlDocument oXmlDoc = new XmlDocument(); HttpWebRequest oHttpRequest = null; StreamReader oReader = null; string estado = ""; sSoap.Append("<soapenv:Envelope xmlns:soapenv=" + Convert.ToChar(34) + "http://schemas.xmlsoap.org/soap/envelope/" + Convert.ToChar(34) + " xmlns:ec=" + Convert.ToChar(34) + "http://ec.gob.sri.ws.autorizacion" + ((char)34) + "><soapenv:Header/>"); sSoap.Append("\n <soapenv:Body>"); sSoap.Append("\n <ec:autorizacionComprobante>"); sSoap.Append("\n <claveAccesoComprobante>"+ sClaveAcceso.Trim() + "</claveAccesoComprobante>"); sSoap.Append("\n </ec:autorizacionComprobante>"); sSoap.Append("\n </soapenv:Body>"); sSoap.Append("\n</soapenv:Envelope>"); try { oHttpRequest = (HttpWebRequest)WebRequest.Create(this.sUrl); oHttpRequest.ContentType = "text/xml;action=\"" + this.sSoapAction; oHttpRequest.Method = "POST"; oXmlDoc.LoadXml(sSoap.ToString()); oXmlDoc.Save(oHttpRequest.GetRequestStream()); oReader = new StreamReader(oHttpRequest.GetResponse().GetResponseStream()); xmlResultado = oReader.ReadToEnd(); XmlDocument docxmlSRI = new XmlDocument(); docxmlSRI.LoadXml(xmlResultado); xmlResultado = WebUtility.HtmlDecode(xmlResultado); //Cargo a un XML XmlDocument docxml = new XmlDocument(); var caracterPrincipal = xmlResultado.IndexOf('?') - 1; var caracterSecundario = xmlResultado.LastIndexOf('?') + 2; xmlResultado = xmlResultado.Remove(caracterPrincipal, (caracterSecundario - caracterPrincipal)); xmlResultado = "<?xml version=" + "\"1.0\"" + " encoding=" + "\"UTF-8\"" + "?>" + xmlResultado; docxml.LoadXml(xmlResultado); //Preguntar sobre el directorio string FechaAutorizacion = "", mensaje = "", infoadicional = "", tipo = "", claveconsultada = "", numeroAutorizacion = "", claveAcceso = ""; estado = docxml.GetElementsByTagName("estado")[0].InnerText; string ambiente = docxml.GetElementsByTagName("ambiente")[0].InnerText; claveconsultada = docxml.GetElementsByTagName("claveAccesoConsultada")[0].InnerText; if (estado == "AUTORIZADO") { numeroAutorizacion = docxml.GetElementsByTagName("numeroAutorizacion")[0].InnerText; claveAcceso = docxml.GetElementsByTagName("claveAcceso")[0].InnerText; numeroAutorizacion = docxml.GetElementsByTagName("numeroAutorizacion")[0].InnerText; //Fecha para autorizar FechaAutorizacion = Convert.ToDateTime(docxml.GetElementsByTagName("fechaAutorizacion")[0].InnerText).ToString(); estado += ";" + FechaAutorizacion; if (!Directory.Exists(PathServer + @"\Autorizados\")) { Directory.CreateDirectory(PathServer + @"\Autorizados\"); } docxmlSRI.Save(@PathServer + @"\Autorizados\" + @"\" + sClaveAcceso + ".xml"); MessageBox.Show("Estado: " + estadorecepcion + "\nAutorización: " + estado + "\nAmbiente: " + ambiente + "\nClave Acceso: " + claveAcceso + "\nNumero de Autorización: " + numeroAutorizacion + "\nFecha Autorización: " + FechaAutorizacion); } else { string CodigoError = docxml.GetElementsByTagName("identificador")[0].InnerText; tipo = docxml.GetElementsByTagName("tipo")[0].InnerText; mensaje = docxml.GetElementsByTagName("mensaje")[1].InnerText; try { infoadicional = docxml.GetElementsByTagName("informacionAdicional")[0].InnerText; infoadicional = Funcion.reemplazarcaracterSignosNopermitidos(infoadicional); } catch (Exception ex) { infoadicional = ""; } string fechaEmision = docxml.GetElementsByTagName("fechaEmision")[0].InnerText; if (!Directory.Exists(PathServer + @"\No Autorizados\")) { Directory.CreateDirectory(PathServer + @"\No Autorizados\"); } docxmlSRI.Save(@PathServer + @"\No Autorizados\" + @"\" + sClaveAcceso + ".xml"); string rutaNAT = @PathServer + @"\No Autorizados\"; string fechafinal = Funcion.reemplazarcaracterFecha(fechaEmision); if (objConsultas.ExisteEnFacturaElectro("NombreXML", sClaveAcceso, "TbErroresDocEnviados")) { objConsultas.EjecutarSQLFactElectronica("UPDATE [dbo].[TbErroresDocEnviados] SET [Ruta] = '" + rutaNAT + "' ,[FechaEmision] ='" + fechafinal + "',[EstadoError] = '" + 1 + "',[MensajeError] = '" + mensaje + "' ,[CodigoError] ='" + CodigoError + "' ,[InformacionAdicional] = '" + infoadicional + "' WHERE = '" + sClaveAcceso + "'" + "'"); } else { objConsultas.EjecutarSQLFactElectronica("INSERT INTO [dbo].[TbErroresDocEnviados]([NombreXML],[Ruta],[FechaEmision] ,[EstadoError],[InformacionAdicional],[CodigoError],[MensajeError] )" + "VALUES ('" + sClaveAcceso + "','" + rutaNAT + "','" + fechafinal + "','" + 1 + "','" + infoadicional + "','" + CodigoError + "','" + mensaje + "')"); } MessageBox.Show("Estado: " + estadorecepcion + "\nAutorización: " + estado + "\nAmbiente: " + ambiente + "\nClave Acceso: " + claveAcceso + "\nError: " + mensaje + "\n:Info. Adicional: " + infoadicional); } } catch (Exception ex) { sResult = ex.Message; } return(estado); }
public string RecepcionArchivos(string pathDocumento, string urlDocumeto, string nombreArchivo, string PathServer) { //string PathServer = @"C:\ArchivosXml"; _URLWS = urlDocumeto; byte[] bytes = File.ReadAllBytes(pathDocumento); object RESULTADO = ""; //convertir el archivo en formato 64 bytes RESULTADO = Convert.ToBase64String(bytes); HttpWebRequest request = CreateWebRequest(); XmlDocument soapEnvelopeXml = new XmlDocument(); soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ec=""http://ec.gob.sri.ws.recepcion""> <soapenv:Header/> <soapenv:Body> <ec:validarComprobante> <!--Optional:--> <xml>" + RESULTADO + "</xml>" + "</ec:validarComprobante>" + "</soapenv:Body>" + "</soapenv:Envelope>"); string soapResult = ""; try { using (Stream stream = request.GetRequestStream()) { soapEnvelopeXml.Save(stream); } try { using (WebResponse response = request.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { _xmlResutado = rd.ReadToEnd(); } } } catch (Exception ex) { soapResult = "Error en enviar al SRI" + ex.Message; RecepcionArchivos(pathDocumento, urlDocumeto, nombreArchivo, PathServer); } } catch (Exception x) { soapResult = "Error en enviar al SRI" + x.Message; } XmlDocument docxml = new XmlDocument(); docxml.Load(pathDocumento); XmlDocument docxml1 = new XmlDocument(); docxml1.LoadXml(_xmlResutado); string estado = docxml1.GetElementsByTagName("estado")[0].InnerText; //Preguntar sobre el directorio if (estado == "RECIBIDA") { if (!Directory.Exists(PathServer + @"\Recibidos\")) { Directory.CreateDirectory(PathServer + @"\Recibidos\"); } docxml.Save(@PathServer + @"\Recibidos\" + @"\" + nombreArchivo + ".xml"); } else { string CodigoError = docxml1.GetElementsByTagName("identificador")[0].InnerText; string mensaje = docxml1.GetElementsByTagName("mensaje")[1].InnerText; estado = estado + ";" + mensaje; string informacionAdicional = ""; try { informacionAdicional = docxml1.GetElementsByTagName("informacionAdicional")[0].InnerText; informacionAdicional = Funcion.reemplazarcaracterSignosNopermitidos(informacionAdicional); } catch (Exception ex) { informacionAdicional = ""; } if (!Directory.Exists(PathServer + @"\Devueltos\")) { Directory.CreateDirectory(PathServer + @"\Devueltos\"); } docxml.Save(@PathServer + @"\Devueltos\" + @"\" + nombreArchivo + ".xml"); string rutaNAT = @PathServer + @"\Devueltos\"; string fechafinal = Funcion.reemplazarcaracterFecha(DateTime.Now.Date.ToShortDateString()); if (objConsultas.ExisteEnFacturaElectro("NombreXML", nombreArchivo, "TbErroresDocEnviados")) { objConsultas.EjecutarSQLFactElectronica("UPDATE [dbo].[TbErroresDocEnviados] SET [Ruta] = '" + rutaNAT + "' ,[FechaEmision] ='" + fechafinal + "',[EstadoError] = '" + 1 + "',[MensajeError] = '" + mensaje + "' ,[CodigoError] ='" + CodigoError + "' ,[InformacionAdicional] = '" + informacionAdicional + "' WHERE NombreXML = '" + nombreArchivo + "'"); } else { objConsultas.EjecutarSQLFactElectronica("INSERT INTO [dbo].[TbErroresDocEnviados]([NombreXML],[Ruta],[FechaEmision] ,[EstadoError],[CodigoError],[MensajeError],[InformacionAdicional] )" + "VALUES ('" + nombreArchivo + "','" + rutaNAT + "','" + fechafinal + "','" + 1 + "','" + CodigoError + "','" + mensaje + "','" + informacionAdicional + "')"); } MessageBox.Show("Estado: " + estado + "\nClave Acceso: " + nombreArchivo + "\nError: " + mensaje); } if (!Directory.Exists(PathServer + @"\Enviados\")) { Directory.CreateDirectory(PathServer + @"\Enviados\"); } docxml.Save(@PathServer + @"\Enviados\" + @"\" + nombreArchivo + ".xml"); return(estado); }