public IHttpActionResult GetData(string captcha, string dni) { RootObjectSIS response = new RootObjectSIS(); try { var txtCapcha = driver.FindElement(By.Name("CaptchaControl1")); txtCapcha.SendKeys(captcha); var txtNroDocumento = driver.FindElement(By.Id("txtNroDocumento")); txtNroDocumento.SendKeys(dni); var btnBuscar = driver.FindElement(By.Name("btnConsultar")); btnBuscar.Click(); var existeData = driver.FindElement(By.XPath("//*[@id=\"lblMensaje\"]")).Text == string.Empty; ResultSIS _ResultSIS = new ResultSIS(); if (existeData) { _ResultSIS.ApellidoMaterno = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/ tbody/tr[2]/td[6]")).First().Text; _ResultSIS.ApellidoPaterno = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/tbody/tr[2]/td[5]")).First().Text; _ResultSIS.NroAfiliacion = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/tbody/tr[2]/td[3]")).First().Text; _ResultSIS.Nombres = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/ tbody/tr[2]/td[7]")).First().Text; _ResultSIS.TipoAsegurado = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/tbody/tr[2]/td[8]")).First().Text; _ResultSIS.Estado = driver.FindElements(By.XPath("//*[@id=\"dgConsulta\"]/tbody/tr[2]/td[9]")).First().Text; response.BaseDatos = false; response.Error = false; response.Status = (int)HttpStatusCode.OK; response.Result = _ResultSIS; } else { response.Error = true; response.Status = (int)HttpStatusCode.NoContent; response.Message = "El paciente no se encuentra registrado en el SIS."; } return(Ok(response)); } catch (Exception ex) { response.Error = true; response.Status = (int)HttpStatusCode.BadRequest; response.Message = "Codigo captcha incorrecto."; return(Ok(response)); } }
public IHttpActionResult GetData(string captcha, string value, int tipoBusqueda) { RootObjectSIS response = new RootObjectSIS(); try { var tipoPersona = value.Substring(0, 1) == "2" ? "1" : "0"; var myUrl = string.Format("http://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS00Alias?accion=consPorRuc&nroRuc={0}&codigo={1}&tipdoc={2}", value, captcha, tipoPersona); var myWebRequest = (HttpWebRequest)WebRequest.Create(myUrl); myWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";//esto creo que lo puse por gusto :/ myWebRequest.CookieContainer = _myCookie; myWebRequest.Credentials = CredentialCache.DefaultCredentials; myWebRequest.Proxy = null; myWebRequest.ContentType = "text/xml;charset=\"utf-8\""; var myHttpWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); var myStream = myHttpWebResponse.GetResponseStream(); if (myStream == null) { response.Error = true; response.Status = (int)HttpStatusCode.BadRequest; response.Message = "Error Server"; return(Ok(response)); } var myStreamReader = new StreamReader(myStream, Encoding.GetEncoding("ISO-8859-1")); var s = myStreamReader.ReadToEnd(); HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(s); var tabla = document.DocumentNode.SelectSingleNode("//*[contains(@class,'form-table')]"); if (tabla == null) { response.Error = true; response.Status = (int)HttpStatusCode.BadRequest; response.Message = "Error Captcha"; return(Ok(response)); } if (tipoBusqueda == 1)//RUC { var busquedaRuc = driver.FindElement(By.Id("btnPorRuc")); busquedaRuc.Click(); var txtRuc = driver.FindElement(By.Id("txtRuc")); txtRuc.SendKeys(value); } else if (tipoBusqueda == 2)//DNI { var busquedaDni = driver.FindElement(By.Id("btnPorDocumento")); busquedaDni.Click(); var txtNumeroDocumento = driver.FindElement(By.Id("txtNumeroDocumento")); txtNumeroDocumento.SendKeys(value); } else if (tipoBusqueda == 3)//NOMBRE { var busquedaNombre = driver.FindElement(By.Id("btnPorRazonSocial")); busquedaNombre.Click(); var txtNombreRazonSocial = driver.FindElement(By.Id("txtNombreRazonSocial")); txtNombreRazonSocial.SendKeys(value); } var txtCodigo = driver.FindElement(By.Name("codigo")); txtCodigo.SendKeys(captcha); var btnBuscar = driver.FindElement(By.Id("btnAceptar")); var testo = btnBuscar.Text; btnBuscar.Click(); var errorText = driver.FindElement(By.ClassName("error")); if (errorText != null) { response.Error = true; response.Status = (int)HttpStatusCode.BadRequest; response.Message = errorText.Text; return(Ok(response)); } var existeData = driver.FindElement(By.ClassName("panel-heading")).Text; ResultSIS _ResultSIS = new ResultSIS(); if (existeData == "Resultado de la Búsqueda") { response.BaseDatos = false; response.Error = false; response.Status = (int)HttpStatusCode.OK; response.Result = _ResultSIS; } else if (existeData == "Relación de contribuyentes") { var anchorLink = driver.FindElement(By.XPath("//*[@class=\"list-group-item\"]")); anchorLink.Click(); if (tipoBusqueda == 1) { } else { response.Error = true; response.Status = (int)HttpStatusCode.NoContent; response.Message = "El paciente no se encuentra registrado en el SIS."; } } else { response.Error = true; response.Status = (int)HttpStatusCode.NoContent; response.Message = "El paciente no se encuentra registrado en el SIS."; } return(Ok(response)); } catch (Exception ex) { response.Error = true; response.Status = (int)HttpStatusCode.BadRequest; response.Message = "Codigo captcha incorrecto."; return(Ok(response)); } }