开发者

Understanding Asynchronous HttpWebRequest

开发者 https://www.devze.com 2022-12-15 02:00 出处:网络
I\'m trying to figure out how this works exactly.开发者_开发知识库 HttpWebRequest has a method BeginGetResponse which accepts as a parameter a ResponseCallback. Firstly, is this callback called immed

I'm trying to figure out how this works exactly.

开发者_开发知识库

HttpWebRequest has a method BeginGetResponse which accepts as a parameter a ResponseCallback. Firstly, is this callback called immediately (in a new thread) or once it receives a response back from the server? Or is it the job of EndGetResponse to wait for the response?

Secondly, once you have the response, you can access the response stream immediately, but the stream doesn't contain the full page until it's completed downloading, hence BeginRead. However, I seem to be able to access all the headers immediately, through properties like HttpWebResponse.ContentLength. Does this mean that EndGetResponse doesn't complete until the headers are fully downloaded, or is it that when you call the ContentLength parameter it hangs for a bit until that header is received?


The Begin* and End* pattern is always the same:

  • Pass in an AsyncCallback to Begin
  • Wait for the call of that callback (not immediate)
  • Call End* always in the callback
  • Check the success of the callback/IAsyncResult (Was it cancelled? Any exceptions/errors?)
  • Use the result from End* here..

The result in your case is a WebResponse instance, which contains the Headers. So I think it's safe to assume that that part of the response was already received: The WebResponse object has no async magic hidden in its properties. If you access the ContentLength it's just a regular property and the object doesn't know or care if you got it using a blocking or an asynchronous call.

0

精彩评论

暂无评论...
验证码 换一张
取 消