在VB.NET中如何调用异步Web请求? [英] How do you call an Asynchronous Web Request in VB.NET?

查看:33
本文介绍了在VB.NET中如何调用异步Web请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码来创建网络请求:

I am currently using the following code to create a web request:

Dim myRequest As WebRequest = WebRequest.Create("http://foo.com/bar")
Dim myResponse As WebResponse = myRequest.GetResponse()

问题是这会锁定"程序直到请求完成(如果请求永远不会完成,程序将挂起).您如何将这样的事情更改为异步执行,以便在 Web 请求完成时可以完成其他任务?

The problem is that this "locks" up the program until the request is completed (and program will hang if the request never completes). How do you change something like this to execute asynchronously so that other tasks can be completed while the web request completes?

推荐答案

您将使用 BeginGetResponse 添加一个 AsyncCallback,它基本上指向代码中的一些其他方法,当 WebRequest 返回时将调用这些方法.有一个很好的示例.

You'll use BeginGetResponse to add a AsyncCallback, which basically points to some other method in your code that will be called when the WebRequest returns. There is a good sample here.

http://www.sitepoint.com/forums/showpost.php?p=3753215

这篇关于在VB.NET中如何调用异步Web请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆