在执行xmlhttp请求时发布表单数据的一部分

时间:2019-07-15 09:07:16

标签: excel vba post xmlhttprequest

在下面的代码中,我尝试使用POST方法,并且我发现它正在使用具有非常长文本的Form Data

Sub Test()
Dim http        As New XMLHTTP60
Dim html        As New HTMLDocument
Dim post        As Object
Dim myUrl       As String
Dim postData    As String


myUrl = "https://www.e.gov.kw/sites/kgoArabic/Pages/eServices/PACI/CivilIDStatus.aspx"
postData = "ctl00%24m%24g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3%24ctl00%24txtCivilID=270022102796"

With http
    .Open "POST", myUrl, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send postData
    html.body.innerHTML = .responseText

    WriteTxtFile html.body.innerHTML
End With

End Sub

Sub WriteTxtFile(ByVal aString As String, Optional ByVal filePath As String = "C:\Users\Future\Desktop\Output.txt")
Dim fso         As Object
Dim fileout     As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set fileout = fso.CreateTextFile(filePath, True, True)
fileout.Write aString
fileout.Close
End Sub

我正在尝试搜索ID 270022102796,它位于表单数据ctl00%24m%24g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3%24ctl00%24txtCivilID=270022102796的此部分中。 在呈现的html中,我找不到此条目的预期输出,该条目应位于ID为ctl00_m_g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3_ctl00_lblResult

的元素中

是否可以仅使用部分表单数据?如果可能的话,为什么我没有得到正确的HTML文档?

  • 为使问题更清楚,我有使用IE的解决方案

      Sub IDQuery()
    
     Dim doc As HTMLDocument
     Dim ie As Object
     Dim r As Integer
     Dim i As Integer
    
     Set ie = CreateObject("InternetExplorer.Application")
    
     With ie
      .Visible = True
    
      For r = 2 To Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
         If r = 2 Then
               .navigate "https://www.e.gov.kw/sites/kgoArabic/Pages/eServices/PACI/CivilIDStatus.aspx"
            Application.Wait (Now + TimeValue("0:00:05"))
            Do: DoEvents: Loop Until ie.readyState = READYSTATE_COMPLETE
    
            Set doc = ie.document
            While ie.readyState <> 4: DoEvents: Wend
        End If
    
        doc.getElementById("ctl00_m_g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3_ctl00_txtCivilID").Value = Worksheets(1).Cells(r, 1).Value
        doc.getElementById("ctl00_m_g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3_ctl00_btnSearch").Click
    
        Application.Wait (Now + TimeValue("0:00:10"))
    
        Worksheets(1).Cells(r, 2).Value = doc.getElementById("ctl00_m_g_cca43156_d33a_4ef1_8782_2c3c7a4eeaf3_ctl00_lblResult").innerText
    Next r
    
    ie.Quit
    End With
    End Sub
    

0 个答案:

没有答案