I have trouble following the examples to get the 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 have been bangin my head against this all day, I can see the get request in my browser, or with client in fiddler/wireshark. But the code (ReadWebRequestCallback) does not get 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}
Thanks so much for all the help!
I ended up with doing this like described in Simplify Async networking with Tasks in SL5 with tasks.
I do however think the problem relied in that my logging did not log when it was in the callback, this I cannot understand. But after banging my head to the wall for a day, I’ll leave that behind. But think my actual post would work.