responseObject
我有一个这样的方法,在使用 Task.Factory.FromAsync()声明时挂起
private async Task<string> post(string url, string postdata)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
// this works
Task<Stream> requestStream = Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request);
var sw = new StreamWriter(requestStream.Result);
byte[] data = Encoding.UTF8.GetBytes(postdata);
await requestStream.Result.WriteAsync(data, 0, data.Length);
// this hangs
Task<WebResponse> responseObject = Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request); // Hangs here
// Doesn't get to here
var responseStream = responseObject.Result.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}
如果我手动运行 Begin/End 方法,它工作正常,如下所示:
request.BeginGetRequestStream(async ar =>
{
var requestStream = request.EndGetRequestStream(ar);
using (var sw = new StreamWriter(requestStream))
{
byte[] data = Encoding.UTF8.GetBytes(postdata);
await requestStream.WriteAsync(data, 0, data.Length);
}
request.BeginGetResponse(async a =>
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a);
var responseStream = response.GetResponseStream();
using (var sr = new StreamReader(responseStream))
{
string received = await sr.ReadToEndAsync();
}
}, null);
}, null);
但是,在这种情况下,ReadToEndAsync() 在单独的线程上运行,并且我希望该方法返回结果,这是不可能的,因为该方法在 BeginGetResponse 回调完成之前返回。
在这一点上,我认为我做的非常错误并且完全错误的方式,所以任何帮助将不胜感激。