'INIT
lblMensaje.Text = "Inicializando.."
Dim tempCookie As New CookieContainer()
Dim postData As String = "option=credential&Ecom_User_ID=" + txtUsuario.Text + "&Ecom_Password=" + txtPassword.Text + "&submit=Enviar"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
'OBTENEMOS LA COOKIE
lblMensaje.Text = "Obteniendo la Cookie.."
Dim peticionGalleta As HttpWebRequest = CType(WebRequest.Create("https://cfdiau.sat.gob.mx/nidp/app/login?id=SATUPCFDiCon&sid=0&option=credential&sid=0"), HttpWebRequest)
peticionGalleta.Method = "POST"
peticionGalleta.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
'peticionGalleta.ContentType = "text/html;charset=UTF-8";
peticionGalleta.CookieContainer = tempCookie
peticionGalleta.Host = "cfdiau.sat.gob.mx"
peticionGalleta.Referer = "https://cfdiau.sat.gob.mx/nidp/wsfed/ep?id=SATx509Custom&sid=0&option=credential&sid=0"
peticionGalleta.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
'peticionGalleta.ContentLength = byteArray.Length;
Dim sw As New StreamWriter(peticionGalleta.GetRequestStream())
sw.Write(postData, 0, byteArray.Length)
sw.Close()
Dim respuestaGalleta As HttpWebResponse = CType(peticionGalleta.GetResponse(), HttpWebResponse)
Dim lectorGalleta As New StreamReader(respuestaGalleta.GetResponseStream())
Dim htmlr As String = lectorGalleta.ReadToEnd()
richTextBox1.Text = htmlr
WebBrowser1.DocumentText = richTextBox1.Text
'YA CON LA COOKIE INICIAMOS SESION
lblMensaje.Text = "Iniciando Sesion.."
Dim peticion As HttpWebRequest = CType(WebRequest.Create("https://cfdiau.sat.gob.mx/nidp/app/login?sid=0&sid=0"), HttpWebRequest)
peticion.Method = "POST"
peticion.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
peticion.Credentials = CredentialCache.DefaultCredentials
peticion.KeepAlive = True
peticion.CookieContainer = tempCookie
peticion.Host = "cfdiau.sat.gob.mx"
peticion.Referer = "https://cfdiau.sat.gob.mx//nidp/app/login?id=SATUPCFDiCon&sid=0&option=credential&sid=0"
peticion.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
peticion.ContentType = "application/x-www-form-urlencoded"
peticion.ContentLength = byteArray.Length
Dim sw2 As New StreamWriter(peticion.GetRequestStream())
sw2.Write(postData, 0, byteArray.Length)
sw2.Close()
'OBTENEMOS INGRESO
lblMensaje.Text = "Obteniendo Datos.."
Dim respuesta As HttpWebResponse = CType(peticion.GetResponse(), HttpWebResponse)
Dim lector As New StreamReader(respuesta.GetResponseStream())
Dim html As String = lector.ReadToEnd()
richTextBox1.Text = html
WebBrowser1.DocumentText = richTextBox1.Text
'OBTENEMOS DATOS PARA REFERENCIAR
lblMensaje.Text = "Iniciando Tipo.."
Dim peticionTIPOD As HttpWebRequest = CType(WebRequest.Create("https://portalcfdi.facturaelectronica.sat.gob.mx/"), HttpWebRequest)
peticionTIPOD.Method = "POST"
peticionTIPOD.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
peticionTIPOD.Credentials = CredentialCache.DefaultCredentials
peticionTIPOD.KeepAlive = True
peticionTIPOD.CookieContainer = tempCookie
peticionTIPOD.Host = "portalcfdi.facturaelectronica.sat.gob.mx"
peticionTIPOD.Referer = "https://cfdicontribuyentes.accesscontrol.windows.net/v2/wsfederation"
peticionTIPOD.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
peticionTIPOD.ContentType = "application/x-www-form-urlencoded"
''peticionTIPOD.ContentLength = byteArray.Length
Dim sw3 As New StreamWriter(peticionTIPOD.GetRequestStream())
sw3.Write(postData, 0, byteArray.Length)
sw3.Close()
'OBTENEMOS CONTROL DE ACCESO
lblMensaje.Text = "Obteniendo Tipos.."
Dim respuesta3 As HttpWebResponse = CType(peticionTIPOD.GetResponse(), HttpWebResponse)
Dim lector3 As New StreamReader(respuesta3.GetResponseStream())
'OBTIENE RESPUESTA FORM EN STRING
Dim html3 As String = lector3.ReadToEnd()
''ELIMINO LA CEDENA <META HTTP-EQUIV="expires" CONTENT="0"> PARA CONVERTIR RESPUESTA EN XML
Dim CadenaEliminar = "<META HTTP-EQUIV=" & """expires""" & " CONTENT=" & """0""" & ">"
html3 = Replace(html3, CadenaEliminar, "")
''Leemos los valores pasando la respuesta XML a un DataSet
Dim srXmlData As New System.IO.StringReader(html3)
Dim DS As New DataSet
DS.ReadXml(srXmlData)
Rwa = DS.Tables("input").Rows(0).Item("value")
Rwresult = DS.Tables("input").Rows(1).Item("value")
Rwctx = DS.Tables("input").Rows(2).Item("value")
richTextBox1.Text = html3
WebBrowser1.DocumentText = richTextBox1.Text
''MUESTRA TIPO DE DESCARGA
Dim postDataA As String = "wa=" & Rwa & "&wresult=" & Rwresult & "&wctx=" & Rwctx
Dim byteArrayA As Byte() = Encoding.UTF8.GetBytes(postDataA)
'lblMensaje.Text = "Iniciando Tipo.."
Dim Peticion4 As HttpWebRequest = CType(WebRequest.Create("https://cfdicontribuyentes.accesscontrol.windows.net/v2/wsfederation"), HttpWebRequest)
Peticion4.Method = "POST"
''Peticion4.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
'Peticion4.Credentials = CredentialCache.DefaultCredentials
Peticion4.AllowAutoRedirect = True
''Peticion4.KeepAlive = True
''Peticion4.CookieContainer = tempCookie
''Peticion4.Host = "cfdicontribuyentes.accesscontrol.windows.net"
''Peticion4.Referer = "https://cfdiau.sat.gob.mx/nidp/app/login?sid=0&sid=0"
Peticion4.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
''Peticion4.ContentType = "application/x-www-form-urlencoded"
Peticion4.ContentLength = byteArrayA.Length
'Dim sw4 As New StreamWriter(Peticion4.GetRequestStream())
'sw4.Write(postDataA, 0, byteArrayA.Length)
'sw4.Close()
Dim requestStream As Stream = Peticion4.GetRequestStream
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(postDataA)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
'OBTENEMOS SALDO Y VIGENCIA
lblMensaje.Text = "Obteniendo Tipos.."
Dim respuesta4 As HttpWebResponse = CType(Peticion4.GetResponse(), HttpWebResponse)
Dim lector4 As New StreamReader(respuesta4.GetResponseStream())
Dim html4 As String = lector4.ReadToEnd()
richTextBox1.Text = html4
WebBrowser1.DocumentText = richTextBox1.Text