我正在开发VBA代码,以帮助保持Excel工作簿与https://www.fel.mx/CFDI33/Presentacion/Usuario/Ingreso.aspx
上的网络发票服务同步我使用Internet Explorer对象开发了一些工作功能,并决定尝试将其迁移到XMLHTTP,以期获得更好的性能和可靠性。我现在正在处理的功能只是登录到Web服务并获取所有已取消发票的列表
截至目前,我可以通过登录屏幕,导航到发票注册表,按“已取消”状态过滤发票列表,然后导航到结果的第二页。我遇到的问题(听起来很傻)正在进入第三页及以后(我知道有更多的页面,我甚至可以使用'最后一页'按钮进入最后一页,但是不是第3页到'最后 - 1')。
对于上下文,这段代码会让我通过登录屏幕:
'Define xmlhttp request and html response objects
Dim xmlhttp_request As Object
Dim html_response As HTMLDocument
Set xmlhttp_request = CreateObject("Msxml2.XMLHTTP")
Set html_response = CreateObject("htmlFile")
'Get FEL login website html. We need this to later URL encode our POST data including the _EVENT params and our login credentials
With xmlhttp_request: .Open "GET", FEL_WEBSITE_LOGIN, False: .send: End With
'Put the xmlhttp responsetext in our html object
html_response.body.innerHTML = xmlhttp_request.responseText
'URL encode our password
encoded_password = WorksheetFunction.EncodeURL(FEL_PASSWORD)
'Prepare our POST data string
'We include the encoded _EVENT params, our login credentials and the login button item
post_data = _
"__VIEWSTATE=" & WorksheetFunction.EncodeURL(html_response.getElementById("__VIEWSTATE").Value) & _
"&__EVENTVALIDATION=" & WorksheetFunction.EncodeURL(html_response.getElementById("__EVENTVALIDATION").Value) & _
"&txtUsuario=" & FEL_USERNAME & _
"&txtCuenta=" & FEL_USERNAME & _
"&txtContrena=" & encoded_password & _
"&btnInicioSesion=Iniciar sesión"
'POST our xmlhttp request including our post data
With xmlhttp_request
.Open "POST", FEL_WEBSITE_LOGIN, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(post_data)
.send (post_data)
End With
然后,这将使我在2017年的每个月的“已取消”状态下进入结果过滤发票的第一页:
With xmlhttp_request: .Open "GET", FEL_WEBSITE_CFDI_REGISTRY, False: .send: End With
html_response.body.innerHTML = xmlhttp_request.responseText
post_data = _
"__VIEWSTATEENCRYPTED=" & _
"&__VIEWSTATE=" & WorksheetFunction.EncodeURL(html_response.getElementById("__VIEWSTATE").Value) & _
"&__EVENTVALIDATION=" & WorksheetFunction.EncodeURL(html_response.getElementById("__EVENTVALIDATION").Value) & _
"&ctl00$ContentPlaceHolder1$ddlMes=Todos" & _
"&ctl00$ContentPlaceHolder1$ddlAno=2017" & _
"&ctl00$ContentPlaceHolder1$ddlEstadoComprobante=1"
With xmlhttp_request
.Open "POST", FEL_WEBSITE_CFDI_REGISTRY, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(post_data)
.send post_data
End With
然后,之后我可以这样做到第二页:
(事实上,我认为我在这里做的是再次过滤结果,然后转到下一页,而不是从之前过滤的结果转到下一页。我认为这是问题的一部分;阅读更多细节)
html_response.body.innerHTML = xmlhttp_request.responseText
post_data = _
"ctl00$ContentPlaceHolder1$ddlDia=Todos" & _
"&ctl00$ContentPlaceHolder1$ddlMes=Todos" & _
"&ctl00$ContentPlaceHolder1$ddlAno=2017" & _
"&__VIEWSTATE=" & WorksheetFunction.EncodeURL(html_response.getElementById("__VIEWSTATE").Value) & _
"&__PREVIOUSPAGE=" & WorksheetFunction.EncodeURL(html_response.getElementById("__PREVIOUSPAGE").Value) & _
"&__EVENTVALIDATION=" & WorksheetFunction.EncodeURL(html_response.getElementById("__EVENTVALIDATION").Value) & _
"&__VIEWSTATEENCRYPTED=" & _
"&ctl00$ContentPlaceHolder1$pagCFDI$btnSiguiente="
With xmlhttp_request
.Open "POST", FEL_WEBSITE_CFDI_REGISTRY, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(post_data)
.send post_data
End With
就我而言。在那之后,我不能为我的生活进入第三页。我以为我可以重新发送相同的POST请求,但它只会再次返回第2页。
你可以想象我刚刚开始涉足XMLHTTP,所以这对我来说仍然有些陌生。
我觉得我需要的是:
有人能看到我错过的东西吗?