HttpWebRequest.BeginGetResponse 不是异步的

发布于 2024-10-28 11:58:27 字数 960 浏览 3 评论 0原文

我有一些代码可以迭代 100 个 url 并从网络请求数据。

看起来像这样

for each url in urls
  Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
  Dim rq = New ReqArgs
  rq.Url= url
  rq.Request = hwr
  Dim res = 
     hwr.BeginGetResponse(New AsyncCallback(AddressOf FinishWebRequest), rq)
  Dim a = 1
next

这看起来不错吗?

为什么 BeginGetresponse 行在进入 dim a=1 之前需要大约 2-3 秒才能完成?

实际上,我进行了调试,发现 FinishWebRequest 过程在到达 Dim a=1 之前完全运行。

那么这是异步的吗?

我没有通过使用异步来赚取任何时间。我是吗?或者有其他方法可以做到这一点吗?

要点是,主子程序应该发出 300 个请求并将控制权返回给 UI,然后随着请求的到来,FinishWebRequest 应该在自己的线程和自己的时间上缓慢地处理它们。

我该如何做这样做吗?

顺便说一句,主子程序在 BackgroundWorker 中运行,但我检查了 BackgroundWorker ,问题是相同的

似乎答案应该是 此处 但它对我不起作用,

我是 WPF 4.0

感谢您的帮助和建议。谢谢

I have some code that iterates a few 100 urls and requests the data from the web.

It looks something like this

for each url in urls
  Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
  Dim rq = New ReqArgs
  rq.Url= url
  rq.Request = hwr
  Dim res = 
     hwr.BeginGetResponse(New AsyncCallback(AddressOf FinishWebRequest), rq)
  Dim a = 1
next

Does this look ok?

How come the BeginGetresponse line takes about 2-3 seconds to complete before going to dim a=1?.

Actually I debugged and I see that the FinishWebRequest procedure runs completely before the Dim a=1 is reached.

So is this async?

I'm not earning any time by using the async. am I? Or is there a different way to do this?

The point is that the main sub should fire off 300 requests and return control to the UI, then the FinishWebRequest should process them slowly on its own thread and own time , as the requests come in.

How do I do that?

Btw, the main sub is running in a BackgroundWorker, but I checked with out the BackgroundWorker and the problem is the same

It seems that the answer should be here but its just not working for me

I'm WPF 4.0

Appreciate your help and advice. Thanks

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

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

发布评论

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

评论(2

音盲 2024-11-04 11:58:27

是的,

问题出在 POST 上,

我现在开始像这样写帖子

Dim ReqStream = hwr.BeginGetRequestStream(New AsyncCallback(AddressOf FinishRequestStream), rq)

,然后我的回调就像这样

Sub FinishRequestStream(ByVal result As IAsyncResult)
    Dim ag = CType(result.AsyncState, ReqArgs)
    Dim postStream = ag.Request.EndGetRequestStream(result)
    Dim PostBytes = Encoding.UTF8.GetBytes(ag.PostText)
    postStream.Write(PostBytes, 0, PostBytes.Length)
    postStream.Close()
    Dim res = ag.Request.BeginGetResponse(New AsyncCallback(AddressOf FinishResponse), ag)
End Sub

希望这对将来的人有帮助

yup

the problem was with the POST

i now start the post writing like this

Dim ReqStream = hwr.BeginGetRequestStream(New AsyncCallback(AddressOf FinishRequestStream), rq)

and then my callback is like this

Sub FinishRequestStream(ByVal result As IAsyncResult)
    Dim ag = CType(result.AsyncState, ReqArgs)
    Dim postStream = ag.Request.EndGetRequestStream(result)
    Dim PostBytes = Encoding.UTF8.GetBytes(ag.PostText)
    postStream.Write(PostBytes, 0, PostBytes.Length)
    postStream.Close()
    Dim res = ag.Request.BeginGetResponse(New AsyncCallback(AddressOf FinishResponse), ag)
End Sub

hope this helps someone in the future

公布 2024-11-04 11:58:27

从另一个问题重新发布此问题。

来自 HttpWebRequest 文档.BeginGetResponse方法

BeginGetResponse 方法需要先完成一些同步设置任务(例如 DNS 解析、代理检测和 TCP 套接字连接),然后该方法才会变为异步。 [...]
在引发错误异常或方法成功之前,可能需要相当长的时间(最多几分钟,具体取决于网络设置)来完成初始同步设置任务。

为了避免等待设置,您可以使用
HttpWebRequest.BeginGetRequestStream 方法< /a>
但请注意:

您的应用程序不能针对特定请求混合同步和异步方法。如果调用 BeginGetRequestStream 方法,则必须使用 BeginGetResponse 方法来检索响应。

Reposting this from another question.

From the documentation on HttpWebRequest.BeginGetResponse Method:

The BeginGetResponse method requires some synchronous setup tasks to complete (DNS resolution, proxy detection, and TCP socket connection, for example) before this method becomes asynchronous. [...]
it might take considerable time (up to several minutes depending on network settings) to complete the initial synchronous setup tasks before an exception for an error is thrown or the method succeeds.

To avoid waiting for the setup, you can use
HttpWebRequest.BeginGetRequestStream Method
but be aware that:

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the BeginGetRequestStream method, you must use the BeginGetResponse method to retrieve the response.

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