Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top