Login into website using MSXML2.XMLHTTP instead of InternetExplorer.Application with VBA

后端 未结 1 992
醉酒成梦
醉酒成梦 2020-12-08 17:52

first time posting,

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

相关标签:
1条回答
  • 2020-12-08 18:27

    It's possible but not that easy.

    First you need to use CreateObject("Msxml2.ServerXMLHTTP.6.0") and not CreateObject("MSXML2.XMLHTTP").

    Then follow these steps:

    1. Open and send a GET to https://www.nfp.fazenda.sp.gov.br/login.aspx
    2. Parse and store the cookie from the response header "Set-Cookie"
    3. Parse and store the __VIEWSTATE, __VIEWSTATEGENERATOR, __EVENTVALIDATION from the HTML response
    4. Build the data for the next query with the values parsed previously and with your user-name/password :

      __EVENTTARGET:""
      __EVENTARGUMENT:""
      __VIEWSTATE:"..."
      __VIEWSTATEGENERATOR:"..."
      __EVENTVALIDATION:"..."
      ctl00$ddlTipoUsuario:"#rdBtnNaoContribuinte"
      ctl00$UserNameAcessivel:"Digite+o+Usuário"
      ctl00$PasswordAcessivel:"x"
      ctl00$ConteudoPagina$Login1$rblTipo:"rdBtnNaoContribuinte"
      ctl00$ConteudoPagina$Login1$UserName:"..."
      ctl00$ConteudoPagina$Login1$Password:"..."
      ctl00$ConteudoPagina$Login1$Login:"Acessar"
      ctl00$ConteudoPagina$Login1$txtCpfCnpj:"Digite+o+Usuário"
      
    5. Open a POST to https://www.nfp.fazenda.sp.gov.br/login.aspx

    6. Set the header "Cookie" with the cookie parsed at step 2
    7. Set the header Content-Type: "application/x-www-form-urlencoded"
    8. Set the header Content-Length with the length of the data
    9. Send the POST with the data from step 4
    0 讨论(0)
提交回复
热议问题