vb.net异步致电URL,无需响应

发布于 2025-02-12 21:37:42 字数 1102 浏览 0 评论 0原文

我有一个VB.NET页面,该页面需要在用户单击按钮后提交数据。我不需要从URL回来的任何数据,我只需要将参数传递给它,并允许用户继续下一步而无需等待URL完成。

我已经看到了一些针对C#使用uploadStringTaskAsync的帖子,但还没有找到vb.net的相应方法。 https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstringtaskasync?view = net-6.0

我相信我可以从现有的norynync中调用异步方法方法,因为我不需要回复。但是,如果有一种更优雅的方法可以做到这一点,请告诉我。

使用当前代码试图使用线程的更新:

Sub Page_Load(sender As Object, e As EventArgs)
     If Not IsPostBack Then
          Dim thread As New Thread(AddressOf testSub)
          thread.Start()
     End If
End Sub

Sub testSub
     Using WC As New WebClient  WC.UploadString("https://someurl.com?parameter1=testing&parameter2=anothertest", "SOMEDATA")
     End Using
End Sub

这是运行的,但不幸的是似乎无法处理任何参数。当我直接在浏览器中发布URL时,它会按预期运行。我也不需要发送任何数据以外的任何数据,因此我不确定这是否会破坏上传串。但是,当我通过调试器运行它时,只要我填充具有值的数据的字符串,就不会看到任何错误。

但是,当需要等待电话时,我可能会误解。尽管我不需要任何数据,但外部URL最多可能需要5分钟才能处理。我想知道它是否花费太长,并且在开始该线程之后就开始了。

I have a VB.Net page that needs to submit data to a url after a user clicks a button. I don't need any data back from the url, I just need to pass parameters to it and allow the user to continue to the next step without waiting for the url to do it's thing.

I've seen some similar posts for c# using UploadStringTaskAsync, but haven't been able to find a corresponding method for VB.net.
https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstringtaskasync?view=net-6.0

I believe I can call the Async method from my existing nonasync methods as I don't need the response back. However, if there is a more elegant approach to do this please let me know.

Update with current code attempting to use thread:

Sub Page_Load(sender As Object, e As EventArgs)
     If Not IsPostBack Then
          Dim thread As New Thread(AddressOf testSub)
          thread.Start()
     End If
End Sub

Sub testSub
     Using WC As New WebClient  WC.UploadString("https://someurl.com?parameter1=testing¶meter2=anothertest", "SOMEDATA")
     End Using
End Sub

This runs but unfortunately does not appear to handle any of the parameters. When I post the url directly in the browser it runs as expected. I don't need to send any data other than the querystring as well so I'm not sure if that's breaking the uploadstring. However, when I run it via debugger I don't see any errors so long as I populate that string for the data with a value.

I might be misunderstanding though when the await call is needed. While I don't need any data back, the external url can take up to 5 minutes to process. I'm wondering if it's taking too long and timing out after that thread is started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2025-02-19 21:37:42

您可以用自己的线程运行它。

Imports System.Net
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        ' Create a thread to run this in the background
        Dim t As New Thread(
            Sub()
                Using WC As New WebClient
                    WC.UploadString("https://YOURURL", "YOURDATA")
                End Using

            End Sub
        )
        ' Start the thread
        t.Start()

    End Sub


End Class

You could run it in its own thread.

Imports System.Net
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        ' Create a thread to run this in the background
        Dim t As New Thread(
            Sub()
                Using WC As New WebClient
                    WC.UploadString("https://YOURURL", "YOURDATA")
                End Using

            End Sub
        )
        ' Start the thread
        t.Start()

    End Sub


End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文