我无法按照示例获取回调。
我有以下代码:
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
{ }
}
我整天都在努力解决这个问题,我可以在浏览器中看到获取请求,或者在 fiddler/wireshark 中看到客户端。但是代码(ReadWebRequestCallback)没有被调用。
编辑:还要注意,如果我使用 WebClient 和 DownloadStringAsync 它可以工作,但我需要 404 和 200 以外的其他 HTTP 状态代码。:
_client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted);
_client.DownloadStringAsync(_concurrentCheckUrl);
}
private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{// Works, gets here}