Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The response would be available when the callback function RespCallback is invoked. I don't know what <code>reqState</code> has, but I assume it contains a reference to the original HttpWebRequest object. If this is the case, this would be a simple implementation of the RespCallback method:</p> <pre><code>void RespCallback(IAsyncResult asyncResult) { ReqState reqState = (ReqState)asyncResult.AsyncState; HttpWebResponse resp = (HttpWebResponse)reqState.Request.EndGetResponse(asyncResult); // do what you need with the response. } </code></pre> <p><em>Update: more info as asked in the comment</em></p> <p>If you want the response in the same method where you did the Begin call, you can have an event which will be set when the callback is received, and you can wait on that event after the Begin call, like in the example below</p> <pre><code>class ReqState { public HttpWebRequest Request { get; set; } public HttpWebResponse Response { get; set; } public AutoResetEvent Evt { get; set; } } void RespCallback(IAsyncResult asyncResult) { ReqState reqState = (ReqState)asyncResult.AsyncState; reqState.Response = (HttpWebResponse)reqState.Request.EndGetResponse(asyncResult); reqState.Evt.Set(); } void CallMethod() { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(...); // set properties on req ReqState state = new ReqState(); state.Request = req; state.Evt = new ManualResetEvent(false); req.BeginGetResponse(RespCallback, state); state.Evt.WaitOne(TimeSpan.FromSeconds(30)); // wait for 30 seconds // access response via state.Response } </code></pre> <p>Now notice that you're essentially doing a synchronous call in an asynchronous way. That gives you more control over the timeout, but with the price of code complexity. Another thing, this will not work on platforms such as Silverlight (and Windows Phone, IIRC), where synchronous calls (even those dressed up as asynchronous) are forbidden.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload