Do not get callback from BeginGetResponse

I'm having problems with examples for getting a callback.

I have the following code:

private void startWebRequest(object sender, EventArgs e) { Uri url = new Uri("http://localhost.com/dummyGet"); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request); } private void ReadWebRequestCallback(IAsyncResult callbackResult) { Console.WriteLine("Don not get here"); try { var req = (HttpWebRequest)callbackResult.AsyncState; using (var response = req.EndGetResponse(callbackResult)) { Console.WriteLine("Code"); } } catch { } } 

I hit my head about it all day, I see a request for receipt in my browser or with the client in the script / wirehark. But the code (ReadWebRequestCallback) is not called.

Edit: Also note that if I use WebClient and DownloadStringAsync, it works, but I need other HTTP status codes than 404 and 200 .:

 _client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted); _client.DownloadStringAsync(_concurrentCheckUrl); } private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {// Works, gets here} 
+4
source share
2 answers

Thank you for help!

I ended up with this as described in simplifying an asynchronous network using tasks in SL5 with tasks.

 HttpWebRequest _request; private void doGetRequest() _request = WebRequestCreator.ClientHttp.Create(new Uri("http://localhost/getDummy")) as HttpWebRequest; var webTask = Task.Factory.FromAsync<WebResponse> (_request.BeginGetResponse, _request.EndGetResponse, null) .ContinueWith( task => { var response = (HttpWebResponse)task.Result; // The reason I use HttpRequest, not WebRequest, to get statuscode. if (response.StatusCode == HttpStatusCode.ServiceUnavailable) { //Do Stuff } }); 

However, I believe that the problem is that my journal was not recorded when it was in the callback, I can not understand this. But by hitting my head against the wall during the day, I will leave it behind. But think that my actual post will work.

0
source

I'm not sure if this is a solution, but the rights limit is closed until callback is called? Being a silver light, I doubt it, but I thought I would raise it.

Check http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx - pay attention to

 ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true); // The response came in the allowed time. The work processing will happen in the // callback function. allDone.WaitOne(); 

This might be what you should try on Thread.Sleep. If this is not a problem, can you confirm that the code never runs by adding a breakpoint or some other output to be safe?

+1
source

All Articles