如何异步调用asp.net中的函数?或任何其他解决方案来达到目标?
该函数的目标是填写from(来自第3方网站)并单击提交按钮并获取提交结果页面的html源。此任务需要在 asp.net 中按钮的单击事件上完成。如果函数返回 true,则在最后执行一些 sql 任务。我读到了有关 asp.net 中的异步处理程序的内容,但确实是初学者,不确定在 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是使用 HttpHandler 实现长轮询时使用的一些类。我使用此解决方案来处理需要很长时间才能完成的操作。基本上有6个类(见下文)。其中一些课程可能最终对于您的目的来说是不需要的,但它们对我来说是有意义的。这些已“大部分”为您进行了消毒。
如果您需要有关 JavaScript 或 HTML 插件的帮助,请在下面的评论中...我将为您写一些内容。
HTTP 处理程序:
支持类:
Here are some classes used in implementing long-polling using HttpHandlers. I use this solution for operations that take a LONG time to finish. There are basically 6 classes (see below). Some of these classes may end-up being unneeded for YOUR purposes, but they made sense for mine. These have "mostly" been sanitized for you.
If you need help with the JavaScript or HTML add-in a comment below...I will write something for you.
HTTP HANDLERS:
SUPPORTING CLASSES:
看一下这篇文章,它描述了如何异步执行方法,并使用在异步方法调用完成时触发的事件处理程序。
http://www.csharp-examples.net/create-asynchronous-method/
以下是如何将本文内容应用到您的情况的草稿。我还没有测试过代码,所以它可能不完美,但应该很接近。
您需要导入以下命名空间:
这是粗略的实现:
编辑:这是 VB.NET 中的代码 - 并不肯定所有语法都正确
Take a look at this article, which describes how to execute a method asynchronously, and uses an event handler that fires when the asynchronous method call has completed.
http://www.csharp-examples.net/create-asynchronous-method/
Here's a rough draft of how you would apply the contents of the article in your situation. I haven't tested the code, so it might not be perfect, but it should be close.
You need to import the following namespaces:
And here is the rough implementation:
EDIT: Here is the code in VB.NET - not positive that all syntax is correct