title: Login into website using MSXML2.XMLHTTP instead of InternetExplorer.Application with VBA
body: <p>first time posting,</p>
  
  <p>I'm trying to get the ID "dadosDoUsuario" from a website's page I have to be logged in. I got it working using "InternetExplorer.Application" object, but can't get the ID value when using "MSXML2.XMLHTTP" object. It seems it won't go past the login page, since I'm able to get other IDs from this page (example: "tituloPagina"). Could someone give a hint on how I get the data from the page after logged in? Thanks!</p>
  
  <p>InternetExplorer.Application code (this one works):</p>
  
  <pre><code>Sub testIE()
  Dim texto As String
  
  Set ie = CreateObject("InternetExplorer.Application")
  my_url = "https://www.nfp.fazenda.sp.gov.br/login.aspx"
  With ie
      .Visible = False
      .Navigate my_url
  
  Do Until Not ie.Busy And ie.readyState = 4
      DoEvents
  Loop
  
  End With
  
  ie.Document.getelementbyid("userName").Value = "MYUSERNAME"
  ie.Document.getelementbyid("Password").Value = "MYPASSWORD"
  ie.Document.getelementbyid("Login").Click
  
  Do Until Not ie.Busy And ie.readyState = 4
      DoEvents
  Loop
  
  ie.Document.getelementbyid("btnConsultarNFSemestre").Click
  
  Do Until Not ie.Busy And ie.readyState = 4
      DoEvents
  Loop
  
  texto = ie.Document.getelementbyid("dadosDoUsuario").innerText
  MsgBox texto
  
  ie.Quit
  
  End Sub
  </code></pre>
  
  <p>MSXML2.XMLHTTP code (this one doesn't work):</p>
  
  <pre><code>Sub testXMLHTTP()
  Dim xml As Object
  Dim html As Object
  Dim dados As Object
  Dim text As Object
  
  Set xml = CreateObject("MSXML2.XMLHTTP")
  
  Set html = CreateObject("htmlFile")
  
  With xml
    .Open "POST", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
    .setRequestHeader "Content-Type", "text/xml"
    .send "userName=MYUSERNAME&amp;password=MYPASSWORD"
    .Open "GET", "https://www.nfp.fazenda.sp.gov.br/Inicio.aspx", False
    .setRequestHeader "Content-Type", "text/xml"
    .send
  End With
  
  html.body.innerhtml = xml.responseText
  
  Set objResult = html.GetElementById("dadosDoUsuario")
  GetElementById = objResult.innertext
  
  MsgBox GetElementById
  
  End Sub
  </code></pre>
  
  <p>EDIT: I followed the steps suggested by @Florent B., and added a scripcontrol to get the encoded values for __VIEWSTATE, __VIEWSTATEGENERATOR and __EVENTVALIDATION. Got it working!</p>
  
  <pre><code>Sub testXMLHTTP()
  Dim xml As Object
  Dim html As HTMLDocument
  Dim dados As Object
  Dim text As Object
  Dim html2 As HTMLDocument
  Dim xml2 As Object
  
  Set xml = CreateObject("Msxml2.ServerXMLHTTP.6.0")
  Set html = CreateObject("htmlFile")
  
  
  With xml
    .Open "GET", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
    .send
  End With
  
  strCookie = xml.getResponseHeader("Set-Cookie")
  
  html.body.innerhtml = xml.responseText
  
  Set objvstate = html.GetElementById("__VIEWSTATE")
  Set objvstategen = html.GetElementById("__VIEWSTATEGENERATOR")
  Set objeventval = html.GetElementById("__EVENTVALIDATION")
  
  vstate = objvstate.Value
  vstategen = objvstategen.Value
  eventval = objeventval.Value
  
  'URL Encode ViewState
      Dim ScriptEngine As ScriptControl
      Set ScriptEngine = New ScriptControl
      ScriptEngine.Language = "JScript"
      ScriptEngine.AddCode "function encode(vstate) {return encodeURIComponent(vstate);}"
      Dim encoded As String
      encoded = ScriptEngine.Run("encode", vstate)
      vstate = encoded
  'URL Encode Event Validation
      ScriptEngine.AddCode "function encode(eventval) {return encodeURIComponent(eventval);}"
      encoded = ScriptEngine.Run("encode", eventval)
      eventval = encoded
  'URL Encode ViewState Generator
      ScriptEngine.AddCode "function encode(vstategen) {return encodeURIComponent(vstategen);}"
      encoded = ScriptEngine.Run("encode", vstategen)
      vstategen = encoded
  
  Postdata = "__EVENTTARGET=" &amp; "&amp;__EVENTARGUMENT=" &amp; "&amp;__VIEWSTATE=" &amp; vstate &amp; "&amp;__VIEWSTATEGENERATOR=" &amp; vstategen &amp; "&amp;__EVENTVALIDATION=" &amp; eventval &amp; "&amp;ctl00$ddlTipoUsuario=#rdBtnNaoContribuinte" &amp; "&amp;ctl00$UserNameAcessivel=Digite+o+Usuário" &amp; "&amp;ctl00$PasswordAcessivel=x" &amp; "&amp;ctl00$ConteudoPagina$Login1$rblTipo=rdBtnNaoContribuinte" &amp; "&amp;ctl00$ConteudoPagina$Login1$UserName=MYUSERNAME" &amp; "&amp;ctl00$ConteudoPagina$Login1$Password=MYPASSWORD" &amp; "&amp;ctl00$ConteudoPagina$Login1$Login=Acessar" &amp; "&amp;ctl00$ConteudoPagina$Login1$txtCpfCnpj=Digite+o+Usuário"
  
  Set xml2 = CreateObject("Msxml2.ServerXMLHTTP.6.0")
  Set html2 = CreateObject("htmlFile")
  
  With xml2
    .Open "POST", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
    .setRequestHeader "Cookie", strCookie
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .setRequestHeader "Content-Lenght", Len(Postdata)
    .send (Postdata)
  End With
  
  html2.body.innerhtml = xml2.responseText
  
  Set objResult = html2.GetElementById("dadosDoUsuario")
  GetElementById = objResult.innertext
  
  MsgBox GetElementById
  
  
  End Sub
  </code></pre>