gpt4 book ai didi

c# - 不接收来自 BeginGetResponse 的回调

转载 作者:行者123 更新时间:2023-11-30 12:49:31 24 4
gpt4 key购买 nike

我无法按照示例获取回调。

我有以下代码:

 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
{ }
}

我一整天都在反对这个问题,我可以在我的浏览器中看到 get 请求,或者在 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}

最佳答案

我不确定这是否是解决方案,但是在调用回调之前是否关闭了拥有线程?作为 silverlight,我对此表示怀疑,但我想我会提出来。

检查 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx - 注意

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();

这可能是您应该在 Thread.Sleep 上尝试的方法。如果这不是问题所在,为了安全起见,您能否通过添加断点或其他一些输出语句来确认代码永远不会触发?

关于c# - 不接收来自 BeginGetResponse 的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11416579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com