The goal of the function is to fill the from (from 3rd party website) and click the submit button and gets the html source of the submitted result page. This task needs to be done on click event of the button in asp.net. If the function returns true, do some sql tasks at the end. I read about the Asynchronous Handler in asp.net but really beginner of that and not sure what a best solution is to simulate this type of task in asp.net.
Protected Sub lbtnSave_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles lbtnSave.Click
If CrawlWebSite() then
'Save
End If
End Sub
Private Function CrawlWebSite() As Boolean
Dim objBrowser = CreateObject("InternetExplorer.Application")
Dim url As String = "https://test.com/Search.do?subAction=reset&searchType=ind"
With objBrowser
.navigate(url)
System.Threading.Thread.Sleep(1000)
Dim StartTime As DateTime
Dim ElapsedTime As TimeSpan
Dim bLong As Boolean = False
StartTime = Now
Do While .busy = True
ElapsedTime = Now().Subtract(StartTime)
If (ElapsedTime.TotalSeconds Mod 60) >= 55 Then
bLong = True
Exit Do
End If
System.Threading.Thread.Sleep(1000)
Loop
If bLong = True Then
PageName.Alert(Page, "There is a delay retrieving the website for checking NPI. Please try again.")
Return False
End If
.document.getElementById("lastname").Value = txtLastName.Text.Trim
.document.getElementById("searchNpi").Value = txtUPIN.Text.Trim
.document.getElementsByTagName("input").item(7).click()
System.Threading.Thread.Sleep(1000)
bLong = False
StartTime = Now
Do While .busy = True
'There is a delay retrieving the website. Continue ?
ElapsedTime = Now().Subtract(StartTime)
If (ElapsedTime.TotalSeconds Mod 60) >= 50 Then
bLong = True
Exit Do
End If
System.Threading.Thread.Sleep(1000)
Loop
If bLong = True Then
PageName.Alert(Page, "There is a delay retrieving the website. Please try again.")
Return False
End If
If .document.getElementById("lastname") Is Nothing Then
'We have result
Return True
Else
PageName.Alert(Page, "Attention: No matching records found.")
Return False
End If
End With
End Function