我必须向 Web 服务发送异步 POST,并且我想异步发送/检索数据。
我还需要发送请求,但最多只等待 1500 毫秒等待响应。如果我没有得到响应,程序应该继续运行(这是一个进行外部 Web 服务调用的服务)。我想将这些服务调用卸载到 IOCP,而不是长时间阻塞并等待它们返回。我只想阻止总共 1500 毫秒。
这是我到目前为止所拥有的:
var httpRequest = (HttpWebRequest)WebRequest.Create(@"urltoPostTo");
httpRequest.Method = "POST";
byte[] data = Encoding.ASCII.GetBytes("test-post");
httpRequest.ContentLength = data.Length;
var asyncTask = Task.Factory.FromAsync<Stream>(httpRequest.BeginGetRequestStream, httpRequest.EndGetRequestStream, httpRequest)
.ContinueWith(response =>
{
var localStream = response.Result;
if (localStream != null)
{
localStream.Write(data, 0, data.Length);
localStream.Close();
}
}); //how do I do the continuation for BeginGetResponse and EndGetResponse from here?
不幸的是,我有几个要求无法更改。
- 我正在使用面向 4.0 的 Visual Studio 2010
- 我不能使用异步 BCL
- 我想尝试使用 Task.FromAsync