How can I use await with HttpWebRequest in Windows Phone 8 ?
Is there a way to make the IAsyncResult stuff work with await?
private
If you install the Microsoft.Bcl.Async package, you get a few async
-ready extension methods, including ones for HttpWebRequest
.
Use TaskFactory.FromAsync to create a Task<Stream>
from the BeginGetRequestStream
/EndGetRequestStream
methods. Then you can get rid of your OnGotWebRequest
completely, and do the same thing for the response.
Note that currently you're calling EndGetResponse
when a BeginGetRequestStream
call completes, which is inappropriate to start with - you've got to call the EndFoo
method to match the BeginFoo
you originally called. Did you mean to call BeginGetResponse
?